veerukhannan commited on
Commit
11e8c44
·
verified ·
1 Parent(s): 92648a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -3,14 +3,8 @@ 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()
@@ -57,17 +51,26 @@ def get_mistral_analysis(df):
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
 
@@ -76,7 +79,7 @@ def show_nifty_data_and_analysis():
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")
 
3
  from datetime import datetime, timedelta
4
  import requests
5
  import json
 
 
6
  import os
7
 
 
 
 
 
8
  def fetch_nifty_data():
9
  try:
10
  end_date = datetime.now()
 
51
 
52
  data_str += "so i need you to give me the setup for today by priceaction levels"
53
 
54
+ # Mistral API endpoint
55
+ url = "https://api.mistral.ai/v1/chat/completions"
56
+
57
+ headers = {
58
+ "Content-Type": "application/json",
59
+ "Authorization": f"Bearer {os.environ.get('MISTRAL_API_KEY')}"
60
+ }
61
+
62
+ data = {
63
+ "model": "mistral-tiny",
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: {response.status_code} - {response.text}"
73
+
74
  except Exception as e:
75
  return f"Error getting analysis: {str(e)}"
76
 
 
79
  analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
80
  return df, analysis
81
 
82
+ # Create Gradio interface
83
  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")