bluenevus commited on
Commit
1e2b302
·
verified ·
1 Parent(s): e6ca829

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -55
app.py CHANGED
@@ -4,7 +4,6 @@ import matplotlib.pyplot as plt
4
  import io
5
  import base64
6
  import google.generativeai as genai
7
- import traceback
8
 
9
  def process_file(api_key, file, instructions):
10
  # Set up Gemini API
@@ -30,46 +29,33 @@ def process_file(api_key, file, instructions):
30
 
31
  User instructions: {instructions if instructions else 'No specific instructions provided.'}
32
 
33
- Suggest 3 ways to visualize this data. For each visualization:
34
- 1. Describe the visualization type and what it will show.
35
- 2. Provide Python code using matplotlib to create the visualization. Ensure the code is complete and executable.
36
- 3. Explain why this visualization is useful for understanding the data.
37
-
38
  Format your response as:
39
- Visualization 1:
40
- Description: ...
41
- Code:
42
  ```python
 
 
 
 
 
 
 
43
  # Your code here
44
  ```
45
- Explanation: ...
46
-
47
- Visualization 2:
48
- ...
49
-
50
- Visualization 3:
51
- ...
52
  """
53
 
54
  response = model.generate_content(prompt)
55
- suggestions = response.text.split("Visualization")
 
 
 
 
56
 
57
  visualizations = []
58
- for i, suggestion in enumerate(suggestions[1:4], 1): # Process only the first 3 visualizations
59
- parts = suggestion.split("Code:")
60
- description = parts[0].strip()
61
- code_parts = parts[1].split("Explanation:")
62
- code = code_parts[0].strip()
63
- explanation = code_parts[1].strip() if len(code_parts) > 1 else "No explanation provided."
64
-
65
- # Extract code from markdown code block if present
66
- if "```python" in code and "```" in code:
67
- code = code.split("```python")[1].split("```")[0].strip()
68
-
69
- # Execute the code
70
  plt.figure(figsize=(10, 6))
71
  try:
72
- exec(code)
73
  plt.title(f"Visualization {i}")
74
 
75
  # Save the plot to a BytesIO object
@@ -79,14 +65,14 @@ def process_file(api_key, file, instructions):
79
  img_str = base64.b64encode(buf.getvalue()).decode()
80
  plt.close()
81
 
82
- visualizations.append((f"data:image/png;base64,{img_str}", description, code, explanation))
83
  except Exception as e:
84
- error_message = f"Error in visualization {i}: {str(e)}\n{traceback.format_exc()}"
85
- visualizations.append((None, description, code, error_message))
86
 
87
- # Ensure we always return 3 visualizations, even if there were errors
88
  while len(visualizations) < 3:
89
- visualizations.append((None, "Error", "No code generated", "An error occurred"))
90
 
91
  return visualizations
92
 
@@ -102,30 +88,11 @@ with gr.Blocks() as demo:
102
  output1 = gr.Image(label="Visualization 1")
103
  output2 = gr.Image(label="Visualization 2")
104
  output3 = gr.Image(label="Visualization 3")
105
-
106
- with gr.Row():
107
- desc1 = gr.Textbox(label="Description 1")
108
- desc2 = gr.Textbox(label="Description 2")
109
- desc3 = gr.Textbox(label="Description 3")
110
-
111
- with gr.Row():
112
- code1 = gr.Code(language="python", label="Code 1")
113
- code2 = gr.Code(language="python", label="Code 2")
114
- code3 = gr.Code(language="python", label="Code 3")
115
-
116
- with gr.Row():
117
- expl1 = gr.Textbox(label="Explanation/Error 1")
118
- expl2 = gr.Textbox(label="Explanation/Error 2")
119
- expl3 = gr.Textbox(label="Explanation/Error 3")
120
 
121
  submit.click(
122
  fn=process_file,
123
  inputs=[api_key, file, instructions],
124
- outputs=[
125
- output1, desc1, code1, expl1,
126
- output2, desc2, code2, expl2,
127
- output3, desc3, code3, expl3
128
- ],
129
  show_progress=True,
130
  )
131
 
 
4
  import io
5
  import base64
6
  import google.generativeai as genai
 
7
 
8
  def process_file(api_key, file, instructions):
9
  # Set up Gemini API
 
29
 
30
  User instructions: {instructions if instructions else 'No specific instructions provided.'}
31
 
32
+ Generate Python code for 3 different visualizations using matplotlib. Each visualization should be unique and provide insights into the data. Do not include any explanations or descriptions, only the Python code for each visualization.
33
+
 
 
 
34
  Format your response as:
 
 
 
35
  ```python
36
+ # Visualization 1
37
+ # Your code here
38
+
39
+ # Visualization 2
40
+ # Your code here
41
+
42
+ # Visualization 3
43
  # Your code here
44
  ```
 
 
 
 
 
 
 
45
  """
46
 
47
  response = model.generate_content(prompt)
48
+ code = response.text.strip()
49
+
50
+ # Extract code from markdown code block if present
51
+ if "```python" in code and "```" in code:
52
+ code = code.split("```python")[1].split("```")[0].strip()
53
 
54
  visualizations = []
55
+ for i, viz_code in enumerate(code.split("# Visualization")[1:4], 1):
 
 
 
 
 
 
 
 
 
 
 
56
  plt.figure(figsize=(10, 6))
57
  try:
58
+ exec(viz_code, {'df': df, 'plt': plt})
59
  plt.title(f"Visualization {i}")
60
 
61
  # Save the plot to a BytesIO object
 
65
  img_str = base64.b64encode(buf.getvalue()).decode()
66
  plt.close()
67
 
68
+ visualizations.append(f"data:image/png;base64,{img_str}")
69
  except Exception as e:
70
+ print(f"Error in visualization {i}: {str(e)}")
71
+ visualizations.append(None)
72
 
73
+ # Ensure we always return 3 visualizations
74
  while len(visualizations) < 3:
75
+ visualizations.append(None)
76
 
77
  return visualizations
78
 
 
88
  output1 = gr.Image(label="Visualization 1")
89
  output2 = gr.Image(label="Visualization 2")
90
  output3 = gr.Image(label="Visualization 3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  submit.click(
93
  fn=process_file,
94
  inputs=[api_key, file, instructions],
95
+ outputs=[output1, output2, output3],
 
 
 
 
96
  show_progress=True,
97
  )
98