jzou19950715 commited on
Commit
57afec9
·
verified ·
1 Parent(s): 53faa31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -48
app.py CHANGED
@@ -31,7 +31,6 @@ def call_gpt4o_mini(api_key, context, user_prompt):
31
  return "Please provide a valid API key (should start with 'sk-')"
32
 
33
  try:
34
- # Replace with your actual API endpoint
35
  url = os.getenv('GPT4O_MINI_API_URL', 'https://api.openai.com/v1/chat/completions')
36
  headers = {
37
  "Authorization": f"Bearer {api_key}",
@@ -44,7 +43,7 @@ def call_gpt4o_mini(api_key, context, user_prompt):
44
  ]
45
 
46
  payload = {
47
- "model": "gpt-4", # or your specific model
48
  "messages": messages,
49
  "max_tokens": 500,
50
  "temperature": 0.7
@@ -148,20 +147,12 @@ def extended_analysis(df):
148
  except Exception as e:
149
  return output_paths, f"Error during analysis: {str(e)}"
150
 
151
- class ChatHistory(gr.components.Component):
 
152
  def __init__(self):
153
- super().__init__()
154
  self.messages = []
155
  self.data_summary = ""
156
- self._current_df = None
157
-
158
- @property
159
- def current_df(self):
160
- return self._current_df
161
-
162
- @current_df.setter
163
- def current_df(self, df):
164
- self._current_df = df
165
 
166
  def add_message(self, role, content):
167
  self.messages.append({"role": role, "content": content})
@@ -180,19 +171,7 @@ class ChatHistory(gr.components.Component):
180
  def clear(self):
181
  self.messages = []
182
  self.data_summary = ""
183
- self._current_df = None
184
-
185
- def preprocess(self, payload):
186
- """
187
- Preprocess the input data before it is passed to the component.
188
- """
189
- return payload
190
-
191
- def postprocess(self, value):
192
- """
193
- Postprocess the output data before it is returned from the component.
194
- """
195
- return value
196
 
197
  def analyze_and_visualize(file, message, history, api_key, chat_history):
198
  """Main function for data analysis with improved state management"""
@@ -269,38 +248,41 @@ def create_demo():
269
  4. Type keywords like 'visualize', 'plot', 'analyze' to generate charts
270
  """)
271
 
272
- api_key = gr.Textbox(
273
- label="API Key (Optional)",
274
- placeholder="Enter your API key here",
275
- type="password"
276
- )
277
- file_input = gr.File(label="Upload CSV", file_types=[".csv"])
278
- chatbot = gr.Chatbot(height=500, type='messages')
279
- msg = gr.Textbox(
280
- label="Ask about your data",
281
- placeholder="e.g., 'Show me the distribution of scores' or 'What insights can you find?'"
282
- )
283
-
284
- send_btn = gr.Button("Send")
285
- clear_btn = gr.Button("Clear Chat")
286
-
287
- def clear_outputs():
288
- chat_history.clear()
289
- return [], chat_history
290
 
291
  msg.submit(
292
- fn=lambda f, m, h, k, ch: analyze_and_visualize(f, m, h or [], k, ch),
293
  inputs=[file_input, msg, chatbot, api_key, chat_history],
294
  outputs=[chatbot, chat_history]
295
  ).then(lambda: "", None, [msg])
296
 
297
  send_btn.click(
298
- fn=lambda f, m, h, k, ch: analyze_and_visualize(f, m, h or [], k, ch),
299
  inputs=[file_input, msg, chatbot, api_key, chat_history],
300
  outputs=[chatbot, chat_history]
301
  ).then(lambda: "", None, [msg])
302
 
303
- clear_btn.click(fn=clear_outputs, inputs=[], outputs=[chatbot, chat_history])
 
 
 
304
 
305
  demo.queue()
306
  return demo
@@ -308,6 +290,6 @@ def create_demo():
308
  if __name__ == "__main__":
309
  try:
310
  demo = create_demo()
311
- demo.launch(share=True)
312
  finally:
313
  cleanup_temp_files()
 
31
  return "Please provide a valid API key (should start with 'sk-')"
32
 
33
  try:
 
34
  url = os.getenv('GPT4O_MINI_API_URL', 'https://api.openai.com/v1/chat/completions')
35
  headers = {
36
  "Authorization": f"Bearer {api_key}",
 
43
  ]
44
 
45
  payload = {
46
+ "model": "gpt-4",
47
  "messages": messages,
48
  "max_tokens": 500,
49
  "temperature": 0.7
 
147
  except Exception as e:
148
  return output_paths, f"Error during analysis: {str(e)}"
149
 
150
+ class ChatHistory:
151
+ """Simple chat history manager without Gradio component inheritance"""
152
  def __init__(self):
 
153
  self.messages = []
154
  self.data_summary = ""
155
+ self.current_df = None
 
 
 
 
 
 
 
 
156
 
157
  def add_message(self, role, content):
158
  self.messages.append({"role": role, "content": content})
 
171
  def clear(self):
172
  self.messages = []
173
  self.data_summary = ""
174
+ self.current_df = None
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  def analyze_and_visualize(file, message, history, api_key, chat_history):
177
  """Main function for data analysis with improved state management"""
 
248
  4. Type keywords like 'visualize', 'plot', 'analyze' to generate charts
249
  """)
250
 
251
+ with gr.Row():
252
+ with gr.Column():
253
+ api_key = gr.Textbox(
254
+ label="API Key (Optional)",
255
+ placeholder="Enter your API key here",
256
+ type="password"
257
+ )
258
+ file_input = gr.File(label="Upload CSV", file_types=[".csv"])
259
+ msg = gr.Textbox(
260
+ label="Ask about your data",
261
+ placeholder="e.g., 'Show me the distribution of scores' or 'What insights can you find?'"
262
+ )
263
+ with gr.Row():
264
+ send_btn = gr.Button("Send")
265
+ clear_btn = gr.Button("Clear Chat")
266
+
267
+ with gr.Column():
268
+ chatbot = gr.Chatbot(height=600)
269
 
270
  msg.submit(
271
+ fn=analyze_and_visualize,
272
  inputs=[file_input, msg, chatbot, api_key, chat_history],
273
  outputs=[chatbot, chat_history]
274
  ).then(lambda: "", None, [msg])
275
 
276
  send_btn.click(
277
+ fn=analyze_and_visualize,
278
  inputs=[file_input, msg, chatbot, api_key, chat_history],
279
  outputs=[chatbot, chat_history]
280
  ).then(lambda: "", None, [msg])
281
 
282
+ clear_btn.click(
283
+ fn=lambda: ([], ChatHistory()),
284
+ outputs=[chatbot, chat_history]
285
+ )
286
 
287
  demo.queue()
288
  return demo
 
290
  if __name__ == "__main__":
291
  try:
292
  demo = create_demo()
293
+ demo.launch()
294
  finally:
295
  cleanup_temp_files()