jzou19950715 commited on
Commit
02eb377
·
verified ·
1 Parent(s): e279441

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -21
app.py CHANGED
@@ -35,21 +35,22 @@ class DataAnalyzer:
35
  self.current_iteration = 0
36
  self.analysis_results = []
37
 
38
- def call_gpt4o_mini(self, prompt: str) -> str:
39
  """Call GPT-4o-mini API with proper error handling"""
40
  try:
41
- headers = {
42
- "Authorization": f"Bearer {self.api_key}",
43
- "Content-Type": "application/json"
44
- }
45
- response = requests.post(
46
- "https://api.gpt4o-mini.example.com/v1/chat", # Replace with actual endpoint
47
- json={"prompt": prompt, "max_tokens": 500, "temperature": 0.7},
48
- headers=headers,
49
- timeout=15
 
 
50
  )
51
- response.raise_for_status()
52
- return response.json()["choices"][0]["text"]
53
  except Exception as e:
54
  return f"API Error: {str(e)}"
55
 
@@ -165,17 +166,47 @@ class GradioInterface:
165
  self.analyzer = None
166
  self.df = None
167
 
168
- def create_interface(self):
169
- with gr.Blocks() as demo:
170
- gr.Markdown("# Intelligent Data Analysis Agent")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  with gr.Row():
173
- api_key = gr.Textbox(label="GPT-4o-mini API Key", type="password")
174
- file_input = gr.File(label="Upload CSV file")
175
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  with gr.Row():
177
- analysis_notes = gr.Textbox(label="Analysis Notes (Optional)",
178
- placeholder="Any specific analysis preferences...")
 
179
 
180
  with gr.Row():
181
  analyze_btn = gr.Button("Analyze Data")
@@ -184,7 +215,7 @@ class GradioInterface:
184
  output_text = gr.Markdown()
185
  output_gallery = gr.Gallery()
186
 
187
- def analyze(api_key, file, notes):
188
  if not api_key or not file:
189
  return "Please provide both API key and data file.", None
190
 
 
35
  self.current_iteration = 0
36
  self.analysis_results = []
37
 
38
+ def call_gpt4o_mini(self, prompt: str, system_prompt: str) -> str:
39
  """Call GPT-4o-mini API with proper error handling"""
40
  try:
41
+ client = openai.OpenAI(api_key=self.api_key)
42
+ messages = [
43
+ {"role": "system", "content": system_prompt},
44
+ {"role": "user", "content": prompt}
45
+ ]
46
+
47
+ response = client.chat.completions.create(
48
+ model="gpt-4o-mini",
49
+ messages=messages,
50
+ max_tokens=500,
51
+ temperature=0.7
52
  )
53
+ return response.choices[0].message.content
 
54
  except Exception as e:
55
  return f"API Error: {str(e)}"
56
 
 
166
  self.analyzer = None
167
  self.df = None
168
 
169
+ DEFAULT_SYSTEM_PROMPT = """You are an expert data scientist and analyst with the following capabilities:
170
+ 1. Deep understanding of statistical analysis and machine learning
171
+ 2. Ability to identify patterns and insights in data
172
+ 3. Experience with data visualization and interpretation
173
+ 4. Strong technical communication skills to explain complex findings
174
+
175
+ Your task is to:
176
+ 1. Analyze data thoroughly and systematically
177
+ 2. Identify key patterns and relationships
178
+ 3. Suggest appropriate visualizations
179
+ 4. Provide clear interpretations of findings
180
+ 5. Recommend next steps for deeper analysis
181
+
182
+ Always explain your reasoning and provide context for your findings."""
183
+
184
+ def create_interface(self):
185
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
186
+ gr.Markdown("# 🔍 Intelligent Data Analysis Agent")
187
 
188
  with gr.Row():
189
+ with gr.Column(scale=1):
190
+ api_key = gr.Textbox(
191
+ label="GPT-4o-mini API Key",
192
+ type="password",
193
+ placeholder="sk-..."
194
+ )
195
+ file_input = gr.File(
196
+ label="Upload CSV file"
197
+ )
198
+
199
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
200
+ system_prompt = gr.TextArea(
201
+ label="System Prompt",
202
+ value=DEFAULT_SYSTEM_PROMPT,
203
+ lines=8
204
+ )
205
+
206
  with gr.Row():
207
+ analysis_notes = gr.Textbox(
208
+ label="Analysis Notes (Optional)",
209
+ placeholder="Any specific analysis preferences...")
210
 
211
  with gr.Row():
212
  analyze_btn = gr.Button("Analyze Data")
 
215
  output_text = gr.Markdown()
216
  output_gallery = gr.Gallery()
217
 
218
+ def analyze(api_key, file, notes, system_prompt):
219
  if not api_key or not file:
220
  return "Please provide both API key and data file.", None
221