veerukhannan commited on
Commit
22f9e5d
·
verified ·
1 Parent(s): da243b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -32
app.py CHANGED
@@ -54,13 +54,48 @@ def get_mistral_analysis(df):
54
  # Get last 10 days of data
55
  last_10_days = df.tail(10)
56
 
 
 
 
57
  # Format data for prompt
58
- data_str = "this is the last 10 days data for nifty index, Open\tHigh\tLow\tClose\n"
 
 
 
 
 
 
 
 
 
 
59
  for _, row in last_10_days.iterrows():
60
- data_str += f"{row['Open']}\t{row['High']}\t{row['Low']}\t{row['Close']}\n"
61
-
62
- data_str += "so i need you to give me the setup for today by priceaction levels"
63
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # Mistral API endpoint
65
  url = "https://api.mistral.ai/v1/chat/completions"
66
 
@@ -70,18 +105,17 @@ def get_mistral_analysis(df):
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:
@@ -100,27 +134,49 @@ def show_nifty_data_and_analysis():
100
  analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
101
  return df, analysis
102
 
 
 
 
 
 
 
103
  # Create Gradio interface
104
- with gr.Blocks() as demo:
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
-
114
- # Add outputs
115
- with gr.Row():
116
- with gr.Column():
117
- gr.Markdown("### Historical Data")
118
- output_table = gr.Dataframe()
119
-
120
- with gr.Column():
121
- gr.Markdown("### Mistral AI Analysis")
122
- analysis_output = gr.Textbox(label="Price Action Analysis", lines=10)
123
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  # Set up refresh button click event
125
  refresh_btn.click(
126
  fn=show_nifty_data_and_analysis,
@@ -133,4 +189,5 @@ with gr.Blocks() as demo:
133
  outputs=[output_table, analysis_output],
134
  )
135
 
 
136
  demo.launch()
 
54
  # Get last 10 days of data
55
  last_10_days = df.tail(10)
56
 
57
+ # Calculate current price (last close)
58
+ current_price = last_10_days.iloc[-1]['Close']
59
+
60
  # Format data for prompt
61
+ data_str = f"""Based on the following NIFTY 50 price data for the last 10 days, analyze the price action and provide:
62
+ 1. Key support levels (2-3 levels)
63
+ 2. Key resistance levels (2-3 levels)
64
+ 3. Potential target price for today
65
+ 4. Trading setup recommendation (bullish/bearish)
66
+
67
+ Current price: {current_price}
68
+
69
+ Last 10 days data (Open, High, Low, Close):
70
+ """
71
+ # Add data in a readable format
72
  for _, row in last_10_days.iterrows():
73
+ data_str += f"Date: {row['Date']} - Open: {row['Open']}, High: {row['High']}, Low: {row['Low']}, Close: {row['Close']}\n"
74
+
75
+ data_str += """
76
+ Please provide your analysis in the following format:
77
+ Support Levels:
78
+ - S1:
79
+ - S2:
80
+ - S3:
81
+
82
+ Resistance Levels:
83
+ - R1:
84
+ - R2:
85
+ - R3:
86
+
87
+ Today's Target Price:
88
+ - Target:
89
+
90
+ Trading Setup:
91
+ - Direction:
92
+ - Entry Price Range:
93
+ - Stop Loss:
94
+ - Risk/Reward Ratio:
95
+
96
+ Analysis Explanation:
97
+ (Provide brief explanation of the price action and reasoning for levels)"""
98
+
99
  # Mistral API endpoint
100
  url = "https://api.mistral.ai/v1/chat/completions"
101
 
 
105
  }
106
 
107
  data = {
108
+ "model": "mistral-small",
109
+ "messages": [
110
+ {"role": "system", "content": "You are a professional price action trader. Analyze the given market data and provide specific support, resistance levels, and target prices based on price action principles. Be precise with numbers and explain your reasoning."},
111
+ {"role": "user", "content": data_str}
112
+ ],
113
  "temperature": 0.7,
114
+ "max_tokens": 1000
115
  }
116
 
 
117
  response = requests.post(url, headers=headers, json=data)
118
 
 
 
 
119
  if response.status_code == 200:
120
  return response.json()['choices'][0]['message']['content']
121
  else:
 
134
  analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
135
  return df, analysis
136
 
137
+ # Create Gradio interface with custom CSS
138
+ custom_css = """
139
+ .container {max-width: 1200px; margin: auto;}
140
+ .analysis {white-space: pre-wrap; font-family: monospace;}
141
+ """
142
+
143
  # Create Gradio interface
144
+ with gr.Blocks(css=custom_css) as demo:
145
+ with gr.Column(elem_classes="container"):
146
+ gr.Markdown("# NIFTY 50 Analysis with Mistral AI")
147
+ gr.Markdown("Displays the last 15 days of trading data and AI analysis for NIFTY 50 index")
148
+
149
+ # Add API key status
150
+ api_status = gr.Markdown(f"API Status: {verify_api_key()}")
151
+
152
+ # Add refresh button with custom styling
153
+ refresh_btn = gr.Button(
154
+ "Refresh Data and Analysis",
155
+ variant="primary",
156
+ size="lg"
157
+ )
158
+
159
+ # Add outputs in a responsive layout
160
+ with gr.Row():
161
+ with gr.Column(scale=1):
162
+ gr.Markdown("### Historical Data")
163
+ output_table = gr.Dataframe(
164
+ headers=["Date", "Open", "High", "Low", "Close", "Volume"],
165
+ wrap=True
166
+ )
167
+
168
+ with gr.Column(scale=1):
169
+ gr.Markdown("### Mistral AI Analysis")
170
+ analysis_output = gr.Textbox(
171
+ label="Price Action Analysis",
172
+ lines=15,
173
+ elem_classes="analysis"
174
+ )
175
+
176
+ # Add footer
177
+ gr.Markdown("---")
178
+ gr.Markdown("Updated every minute • Data source: Yahoo Finance • Analysis: Mistral AI")
179
+
180
  # Set up refresh button click event
181
  refresh_btn.click(
182
  fn=show_nifty_data_and_analysis,
 
189
  outputs=[output_table, analysis_output],
190
  )
191
 
192
+ # Launch the app
193
  demo.launch()