Update app.py
Browse files
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=
|
167 |
try:
|
168 |
response = requests.get(url)
|
169 |
response.raise_for_status() # Raise exception for HTTP errors
|
170 |
data = response.json()
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
173 |
latest_date = max(trends.keys()) # Get the most recent date
|
174 |
latest_data = trends[latest_date]
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
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
|