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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -163,29 +163,36 @@ def market_analysis_agent(user_input, history=[]):
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=BTC&market=EUR&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
- if "Time Series (Digital Currency Daily)" in data:
172
- trends = data["Time Series (Digital Currency Daily)"]
 
 
 
173
  latest_date = max(trends.keys()) # Get the most recent date
174
  latest_data = trends[latest_date]
175
- return {
176
- "date": latest_date,
177
- "open": latest_data["1a. open (USD)"],
178
- "high": latest_data["2a. high (USD)"],
179
- "low": latest_data["3a. low (USD)"],
180
- "close": latest_data["4a. close (USD)"],
181
- "volume": latest_data["5. volume"],
182
- "market_cap": latest_data["6. market cap (USD)"]
183
- }
 
 
 
 
 
184
  else:
185
  return "No cryptocurrency data available. Check your API key or query parameters."
186
  except Exception as e:
187
  return f"Error fetching cryptocurrency trends: {str(e)}"
188
-
189
  # Gradio Interface setup
190
  chat_interface = gr.Interface(
191
  fn=market_analysis_agent, # Function for handling user interaction
 
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