SatyamSinghal commited on
Commit
e29ab4c
·
verified ·
1 Parent(s): 5ac0b43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -162,37 +162,39 @@ def market_analysis_agent(user_input, history=[]):
162
  except Exception as e:
163
  return [(user_input, f"Oops, something went wrong: {str(e)}")], history
164
 
 
 
165
  def fetch_crypto_trends(symbol="BTC", market="USD", api_key="G3NRIAU5OWJZXS2E"):
166
  url = f'https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol={symbol}&market={market}&apikey={api_key}'
167
  try:
168
  response = requests.get(url)
169
- response.raise_for_status() # Raise exception for HTTP errors
170
  data = response.json()
171
 
172
  # Check if the expected key exists in the response
173
- time_series_key = f"Time Series (Digital Currency Daily)"
174
  if time_series_key in data:
175
  trends = data[time_series_key]
176
  latest_date = max(trends.keys()) # Get the most recent date
177
  latest_data = trends[latest_date]
178
 
179
- # Check if the required keys exist in the latest data
180
- if "1a. open (USD)" in latest_data:
181
- return {
182
- "date": latest_date,
183
- "open": latest_data["1a. open (USD)"],
184
- "high": latest_data["2a. high (USD)"],
185
- "low": latest_data["3a. low (USD)"],
186
- "close": latest_data["4a. close (USD)"],
187
- "volume": latest_data["5. volume"],
188
- "market_cap": latest_data["6. market cap (USD)"]
189
- }
190
- else:
191
- return "Required data (1a. open (USD)) is missing in the response. Check the API response structure."
192
  else:
193
  return "No cryptocurrency data available. Check your API key or query parameters."
194
  except Exception as e:
195
  return f"Error fetching cryptocurrency trends: {str(e)}"
 
 
 
 
196
  # Gradio Interface setup
197
  chat_interface = gr.Interface(
198
  fn=market_analysis_agent, # Function for handling user interaction
 
162
  except Exception as e:
163
  return [(user_input, f"Oops, something went wrong: {str(e)}")], history
164
 
165
+ import requests
166
+
167
  def fetch_crypto_trends(symbol="BTC", market="USD", api_key="G3NRIAU5OWJZXS2E"):
168
  url = f'https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol={symbol}&market={market}&apikey={api_key}'
169
  try:
170
  response = requests.get(url)
171
+ response.raise_for_status() # Raise an exception for HTTP errors
172
  data = response.json()
173
 
174
  # Check if the expected key exists in the response
175
+ time_series_key = "Time Series (Digital Currency Daily)"
176
  if time_series_key in data:
177
  trends = data[time_series_key]
178
  latest_date = max(trends.keys()) # Get the most recent date
179
  latest_data = trends[latest_date]
180
 
181
+ # Extract data based on the market-specific key format
182
+ return {
183
+ "date": latest_date,
184
+ "open": latest_data.get("1. open", "N/A"),
185
+ "high": latest_data.get("2. high", "N/A"),
186
+ "low": latest_data.get("3. low", "N/A"),
187
+ "close": latest_data.get("4. close", "N/A"),
188
+ "volume": latest_data.get("5. volume", "N/A"),
189
+ }
 
 
 
 
190
  else:
191
  return "No cryptocurrency data available. Check your API key or query parameters."
192
  except Exception as e:
193
  return f"Error fetching cryptocurrency trends: {str(e)}"
194
+
195
+ # Example usage
196
+ crypto_data = fetch_crypto_trends(symbol="BTC", market="EUR", api_key="G3NRIAU5OWJZXS2E")
197
+ print(crypto_data)
198
  # Gradio Interface setup
199
  chat_interface = gr.Interface(
200
  fn=market_analysis_agent, # Function for handling user interaction