Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
from mistralai.client import MistralClient
|
7 |
+
from mistralai.models.chat_completion import ChatMessage
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Initialize Mistral AI client
|
11 |
+
# Make sure to set MISTRAL_API_KEY in your Hugging Face Space secrets
|
12 |
+
client = MistralClient(api_key=os.environ.get('MISTRAL_API_KEY'))
|
13 |
+
|
14 |
+
def fetch_nifty_data():
|
15 |
+
try:
|
16 |
+
end_date = datetime.now()
|
17 |
+
start_date = end_date - timedelta(days=15)
|
18 |
+
|
19 |
+
url = f"https://query1.finance.yahoo.com/v8/finance/chart/%5ENSEI?period1={int(start_date.timestamp())}&period2={int(end_date.timestamp())}&interval=1d"
|
20 |
+
|
21 |
+
headers = {
|
22 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
23 |
+
}
|
24 |
+
|
25 |
+
response = requests.get(url, headers=headers)
|
26 |
+
data = response.json()
|
27 |
+
|
28 |
+
timestamps = data['chart']['result'][0]['timestamp']
|
29 |
+
quote = data['chart']['result'][0]['indicators']['quote'][0]
|
30 |
+
|
31 |
+
df = pd.DataFrame({
|
32 |
+
'Date': [datetime.fromtimestamp(ts).strftime('%Y-%m-%d') for ts in timestamps],
|
33 |
+
'Open': quote['open'],
|
34 |
+
'High': quote['high'],
|
35 |
+
'Low': quote['low'],
|
36 |
+
'Close': quote['close'],
|
37 |
+
'Volume': quote['volume']
|
38 |
+
})
|
39 |
+
|
40 |
+
numeric_columns = ['Open', 'High', 'Low', 'Close']
|
41 |
+
df[numeric_columns] = df[numeric_columns].round(2)
|
42 |
+
|
43 |
+
return df
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error fetching data: {str(e)}")
|
46 |
+
return pd.DataFrame()
|
47 |
+
|
48 |
+
def get_mistral_analysis(df):
|
49 |
+
try:
|
50 |
+
# Get last 10 days of data
|
51 |
+
last_10_days = df.tail(10)
|
52 |
+
|
53 |
+
# Format data for prompt
|
54 |
+
data_str = "this is the last 10 days data for nifty index, Open\tHigh\tLow\tClose\n"
|
55 |
+
for _, row in last_10_days.iterrows():
|
56 |
+
data_str += f"{row['Open']}\t{row['High']}\t{row['Low']}\t{row['Close']}\n"
|
57 |
+
|
58 |
+
data_str += "so i need you to give me the setup for today by priceaction levels"
|
59 |
+
|
60 |
+
# Get analysis from Mistral AI
|
61 |
+
messages = [
|
62 |
+
ChatMessage(role="user", content=data_str)
|
63 |
+
]
|
64 |
+
|
65 |
+
response = client.chat(
|
66 |
+
model="mistral-tiny",
|
67 |
+
messages=messages
|
68 |
+
)
|
69 |
+
|
70 |
+
return response.messages[0].content
|
71 |
+
except Exception as e:
|
72 |
+
return f"Error getting analysis: {str(e)}"
|
73 |
+
|
74 |
+
def show_nifty_data_and_analysis():
|
75 |
+
df = fetch_nifty_data()
|
76 |
+
analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
|
77 |
+
return df, analysis
|
78 |
+
|
79 |
+
# Create Gradio interface with auto-refresh
|
80 |
+
with gr.Blocks() as demo:
|
81 |
+
gr.Markdown("# NIFTY 50 Analysis with Mistral AI")
|
82 |
+
gr.Markdown("Displays the last 15 days of trading data and AI analysis for NIFTY 50 index")
|
83 |
+
|
84 |
+
# Add refresh button
|
85 |
+
refresh_btn = gr.Button("Refresh Data and Analysis")
|
86 |
+
|
87 |
+
# Add outputs
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column():
|
90 |
+
gr.Markdown("### Historical Data")
|
91 |
+
output_table = gr.Dataframe()
|
92 |
+
|
93 |
+
with gr.Column():
|
94 |
+
gr.Markdown("### Mistral AI Analysis")
|
95 |
+
analysis_output = gr.Textbox(label="Price Action Analysis", lines=10)
|
96 |
+
|
97 |
+
# Set up refresh button click event
|
98 |
+
refresh_btn.click(
|
99 |
+
fn=show_nifty_data_and_analysis,
|
100 |
+
outputs=[output_table, analysis_output],
|
101 |
+
)
|
102 |
+
|
103 |
+
# Initial load of data
|
104 |
+
demo.load(
|
105 |
+
fn=show_nifty_data_and_analysis,
|
106 |
+
outputs=[output_table, analysis_output],
|
107 |
+
)
|
108 |
+
|
109 |
+
demo.launch()
|