Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import matplotlib.pyplot as plt
|
|
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
|
@@ -31,13 +32,16 @@ def process_file(api_key, file, instructions):
|
|
31 |
|
32 |
Suggest 3 ways to visualize this data. For each visualization:
|
33 |
1. Describe the visualization type and what it will show.
|
34 |
-
2. Provide Python code using matplotlib to create the visualization.
|
35 |
3. Explain why this visualization is useful for understanding the data.
|
36 |
|
37 |
Format your response as:
|
38 |
Visualization 1:
|
39 |
Description: ...
|
40 |
-
Code:
|
|
|
|
|
|
|
41 |
Explanation: ...
|
42 |
|
43 |
Visualization 2:
|
@@ -54,21 +58,35 @@ def process_file(api_key, file, instructions):
|
|
54 |
for i, suggestion in enumerate(suggestions[1:4], 1): # Process only the first 3 visualizations
|
55 |
parts = suggestion.split("Code:")
|
56 |
description = parts[0].strip()
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# Execute the code
|
60 |
plt.figure(figsize=(10, 6))
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
return visualizations
|
74 |
|
@@ -95,13 +113,18 @@ with gr.Blocks() as demo:
|
|
95 |
code2 = gr.Code(language="python", label="Code 2")
|
96 |
code3 = gr.Code(language="python", label="Code 3")
|
97 |
|
|
|
|
|
|
|
|
|
|
|
98 |
submit.click(
|
99 |
fn=process_file,
|
100 |
inputs=[api_key, file, instructions],
|
101 |
outputs=[
|
102 |
-
output1, desc1, code1,
|
103 |
-
output2, desc2, code2,
|
104 |
-
output3, desc3, code3
|
105 |
],
|
106 |
show_progress=True,
|
107 |
)
|
|
|
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
|
|
|
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:
|
|
|
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
|
76 |
+
buf = io.BytesIO()
|
77 |
+
plt.savefig(buf, format='png')
|
78 |
+
buf.seek(0)
|
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 |
|
|
|
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 |
)
|