Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
|
|
| 4 |
import io
|
| 5 |
import google.generativeai as genai
|
| 6 |
from PIL import Image
|
| 7 |
-
import ast
|
| 8 |
|
| 9 |
def process_file(api_key, file, instructions):
|
| 10 |
genai.configure(api_key=api_key)
|
|
@@ -18,27 +18,22 @@ def process_file(api_key, file, instructions):
|
|
| 18 |
except Exception as e:
|
| 19 |
return [None]*3
|
| 20 |
|
| 21 |
-
# Enhanced prompt with strict
|
| 22 |
-
prompt = f"""Generate 3
|
| 23 |
-
1.
|
| 24 |
-
2.
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
3. Different chart types required (bar, line, scatter)
|
| 30 |
-
4. Only use these imports: pandas as pd, matplotlib.pyplot as plt
|
| 31 |
|
| 32 |
Data columns: {list(df.columns)}
|
| 33 |
-
|
| 34 |
User instructions: {instructions or 'None'}
|
| 35 |
|
| 36 |
Format EXACTLY as:
|
| 37 |
# Visualization 1
|
| 38 |
-
|
| 39 |
-
plt.style.use('ggplot')
|
| 40 |
-
# Plot code here
|
| 41 |
-
plt.tight_layout()
|
| 42 |
"""
|
| 43 |
|
| 44 |
response = model.generate_content(prompt)
|
|
@@ -49,50 +44,30 @@ def process_file(api_key, file, instructions):
|
|
| 49 |
buf = io.BytesIO()
|
| 50 |
try:
|
| 51 |
# Advanced code cleaning
|
| 52 |
-
cleaned_code = '\n'.join(
|
| 53 |
-
line.split('#')[0].strip()
|
| 54 |
-
for line in block.split('\n')[1:]
|
| 55 |
-
if line.strip() and
|
| 56 |
-
not any(s in line for s in ['
|
| 57 |
-
|
| 58 |
|
| 59 |
-
#
|
| 60 |
ast.parse(cleaned_code)
|
| 61 |
|
| 62 |
-
# Execute
|
|
|
|
| 63 |
plt.figure(figsize=(16, 9), dpi=120)
|
| 64 |
-
exec(cleaned_code,
|
| 65 |
|
| 66 |
-
# Save image
|
| 67 |
plt.savefig(buf, format='png', bbox_inches='tight')
|
| 68 |
plt.close()
|
| 69 |
buf.seek(0)
|
| 70 |
visualizations.append(Image.open(buf))
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Visualization {i} Error: {str(e)}")
|
| 73 |
-
print(f"
|
| 74 |
visualizations.append(None)
|
| 75 |
|
| 76 |
return visualizations + [None]*(3-len(visualizations))
|
| 77 |
|
| 78 |
-
# Gradio interface
|
| 79 |
-
with gr.Blocks() as demo:
|
| 80 |
-
gr.Markdown("# Data Visualization Tool")
|
| 81 |
-
|
| 82 |
-
with gr.Row():
|
| 83 |
-
api_key = gr.Textbox(label="Gemini API Key", type="password")
|
| 84 |
-
file = gr.File(label="Upload CSV/Excel", file_types=[".csv", ".xlsx"])
|
| 85 |
-
|
| 86 |
-
instructions = gr.Textbox(label="Custom Instructions")
|
| 87 |
-
submit = gr.Button("Generate Visualizations")
|
| 88 |
-
|
| 89 |
-
with gr.Row():
|
| 90 |
-
outputs = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
|
| 91 |
-
|
| 92 |
-
submit.click(
|
| 93 |
-
process_file,
|
| 94 |
-
inputs=[api_key, file, instructions],
|
| 95 |
-
outputs=outputs
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
demo.launch()
|
|
|
|
| 4 |
import io
|
| 5 |
import google.generativeai as genai
|
| 6 |
from PIL import Image
|
| 7 |
+
import ast
|
| 8 |
|
| 9 |
def process_file(api_key, file, instructions):
|
| 10 |
genai.configure(api_key=api_key)
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
return [None]*3
|
| 20 |
|
| 21 |
+
# Enhanced prompt with strict variable requirements
|
| 22 |
+
prompt = f"""Generate 3 matplotlib codes with these rules:
|
| 23 |
+
1. Use EXACTLY these variables: df (DataFrame), plt (matplotlib)
|
| 24 |
+
2. NO imports or additional variables
|
| 25 |
+
3. Start each visualization with:
|
| 26 |
+
plt.figure(figsize=(16,9), dpi=120)
|
| 27 |
+
plt.style.use('ggplot')
|
| 28 |
+
4. End with plt.tight_layout()
|
|
|
|
|
|
|
| 29 |
|
| 30 |
Data columns: {list(df.columns)}
|
| 31 |
+
First 3 rows: {df.head(3).to_dict()}
|
| 32 |
User instructions: {instructions or 'None'}
|
| 33 |
|
| 34 |
Format EXACTLY as:
|
| 35 |
# Visualization 1
|
| 36 |
+
[code using df and plt]
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
|
| 39 |
response = model.generate_content(prompt)
|
|
|
|
| 44 |
buf = io.BytesIO()
|
| 45 |
try:
|
| 46 |
# Advanced code cleaning
|
| 47 |
+
cleaned_code = '\n'.join(
|
| 48 |
+
line.replace('data', 'df').split('#')[0].strip()
|
| 49 |
+
for line in block.split('\n')[1:]
|
| 50 |
+
if line.strip() and
|
| 51 |
+
not any(s in line.lower() for s in ['import', 'data =', 'data='])
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
# Validate syntax
|
| 55 |
ast.parse(cleaned_code)
|
| 56 |
|
| 57 |
+
# Execute with controlled environment
|
| 58 |
+
exec_env = {'df': df, 'plt': plt}
|
| 59 |
plt.figure(figsize=(16, 9), dpi=120)
|
| 60 |
+
exec(cleaned_code, exec_env)
|
| 61 |
|
|
|
|
| 62 |
plt.savefig(buf, format='png', bbox_inches='tight')
|
| 63 |
plt.close()
|
| 64 |
buf.seek(0)
|
| 65 |
visualizations.append(Image.open(buf))
|
| 66 |
except Exception as e:
|
| 67 |
print(f"Visualization {i} Error: {str(e)}")
|
| 68 |
+
print(f"Cleaned Code:\n{cleaned_code}")
|
| 69 |
visualizations.append(None)
|
| 70 |
|
| 71 |
return visualizations + [None]*(3-len(visualizations))
|
| 72 |
|
| 73 |
+
# Rest of the Gradio interface remains the same
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|