Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,12 @@ import requests
|
|
5 |
import json
|
6 |
import os
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def fetch_nifty_data():
|
9 |
try:
|
10 |
end_date = datetime.now()
|
@@ -41,6 +47,10 @@ def fetch_nifty_data():
|
|
41 |
|
42 |
def get_mistral_analysis(df):
|
43 |
try:
|
|
|
|
|
|
|
|
|
44 |
# Get last 10 days of data
|
45 |
last_10_days = df.tail(10)
|
46 |
|
@@ -56,25 +66,36 @@ def get_mistral_analysis(df):
|
|
56 |
|
57 |
headers = {
|
58 |
"Content-Type": "application/json",
|
59 |
-
"Authorization": f"Bearer {
|
60 |
}
|
61 |
|
62 |
data = {
|
63 |
-
"model": "mistral-
|
64 |
-
"messages": [{"role": "user", "content": data_str}]
|
|
|
|
|
65 |
}
|
66 |
|
|
|
67 |
response = requests.post(url, headers=headers, json=data)
|
68 |
|
|
|
|
|
|
|
69 |
if response.status_code == 200:
|
70 |
return response.json()['choices'][0]['message']['content']
|
71 |
else:
|
72 |
-
return f"Error
|
73 |
|
74 |
except Exception as e:
|
75 |
return f"Error getting analysis: {str(e)}"
|
76 |
|
77 |
def show_nifty_data_and_analysis():
|
|
|
|
|
|
|
|
|
|
|
78 |
df = fetch_nifty_data()
|
79 |
analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
|
80 |
return df, analysis
|
@@ -84,6 +105,9 @@ with gr.Blocks() as demo:
|
|
84 |
gr.Markdown("# NIFTY 50 Analysis with Mistral AI")
|
85 |
gr.Markdown("Displays the last 15 days of trading data and AI analysis for NIFTY 50 index")
|
86 |
|
|
|
|
|
|
|
87 |
# Add refresh button
|
88 |
refresh_btn = gr.Button("Refresh Data and Analysis")
|
89 |
|
|
|
5 |
import json
|
6 |
import os
|
7 |
|
8 |
+
def verify_api_key():
|
9 |
+
api_key = os.environ.get('MISTRAL_API_KEY')
|
10 |
+
if not api_key:
|
11 |
+
return "ERROR: Mistral API key not found in environment variables"
|
12 |
+
return "API key found"
|
13 |
+
|
14 |
def fetch_nifty_data():
|
15 |
try:
|
16 |
end_date = datetime.now()
|
|
|
47 |
|
48 |
def get_mistral_analysis(df):
|
49 |
try:
|
50 |
+
api_key = os.environ.get('MISTRAL_API_KEY')
|
51 |
+
if not api_key:
|
52 |
+
return "Error: Mistral API key not found. Please add it to the environment variables."
|
53 |
+
|
54 |
# Get last 10 days of data
|
55 |
last_10_days = df.tail(10)
|
56 |
|
|
|
66 |
|
67 |
headers = {
|
68 |
"Content-Type": "application/json",
|
69 |
+
"Authorization": f"Bearer {api_key}"
|
70 |
}
|
71 |
|
72 |
data = {
|
73 |
+
"model": "mistral-small", # Changed from mistral-tiny to mistral-small
|
74 |
+
"messages": [{"role": "user", "content": data_str}],
|
75 |
+
"temperature": 0.7,
|
76 |
+
"max_tokens": 500
|
77 |
}
|
78 |
|
79 |
+
print(f"Sending request to Mistral AI with API key: {api_key[:5]}...") # Print first 5 chars of API key for debugging
|
80 |
response = requests.post(url, headers=headers, json=data)
|
81 |
|
82 |
+
print(f"Response status code: {response.status_code}")
|
83 |
+
print(f"Response content: {response.text[:200]}...") # Print first 200 chars of response
|
84 |
+
|
85 |
if response.status_code == 200:
|
86 |
return response.json()['choices'][0]['message']['content']
|
87 |
else:
|
88 |
+
return f"Error {response.status_code}: {response.text}"
|
89 |
|
90 |
except Exception as e:
|
91 |
return f"Error getting analysis: {str(e)}"
|
92 |
|
93 |
def show_nifty_data_and_analysis():
|
94 |
+
# First verify API key
|
95 |
+
api_status = verify_api_key()
|
96 |
+
if api_status.startswith("ERROR"):
|
97 |
+
return pd.DataFrame(), api_status
|
98 |
+
|
99 |
df = fetch_nifty_data()
|
100 |
analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
|
101 |
return df, analysis
|
|
|
105 |
gr.Markdown("# NIFTY 50 Analysis with Mistral AI")
|
106 |
gr.Markdown("Displays the last 15 days of trading data and AI analysis for NIFTY 50 index")
|
107 |
|
108 |
+
# Add API key status
|
109 |
+
api_status = gr.Markdown(f"API Status: {verify_api_key()}")
|
110 |
+
|
111 |
# Add refresh button
|
112 |
refresh_btn = gr.Button("Refresh Data and Analysis")
|
113 |
|