Update app.py
Browse files
app.py
CHANGED
@@ -6,83 +6,18 @@ import google.generativeai as genai
|
|
6 |
from PIL import Image
|
7 |
|
8 |
def process_file(api_key, file, instructions):
|
9 |
-
|
10 |
-
model = genai.GenerativeModel('gemini-2.5-pro-latest')
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
if file.name.endswith('.csv'):
|
15 |
-
df = pd.read_csv(file.name)
|
16 |
-
else:
|
17 |
-
df = pd.read_excel(file.name)
|
18 |
-
except Exception as e:
|
19 |
-
return [f"File Error: {str(e)}"] * 3
|
20 |
-
|
21 |
-
# Enhanced prompt with visualization requirements
|
22 |
-
prompt = f"""Generate exactly 3 distinct matplotlib visualization codes for this dataset:
|
23 |
-
Columns: {list(df.columns)}
|
24 |
-
Data types: {dict(df.dtypes)}
|
25 |
-
Sample data: {df.head(3).to_dict()}
|
26 |
-
|
27 |
-
Requirements:
|
28 |
-
1. 1920x1080 resolution (figsize=(16,9), dpi=120)
|
29 |
-
2. Professional styling (seaborn style, grid lines)
|
30 |
-
3. Clear labels and titles
|
31 |
-
4. Diverse chart types (at least 1 must be non-basic)
|
32 |
-
|
33 |
-
User instructions: {instructions or 'No specific instructions'}
|
34 |
-
|
35 |
-
Format response strictly as:
|
36 |
-
# Visualization 1
|
37 |
-
plt.figure(figsize=(16,9), dpi=120)
|
38 |
-
[code]
|
39 |
-
plt.tight_layout()
|
40 |
-
|
41 |
-
# Visualization 2
|
42 |
-
...
|
43 |
-
"""
|
44 |
-
|
45 |
-
response = model.generate_content(prompt)
|
46 |
-
code_blocks = response.text.split("# Visualization ")[1:4] # Force 3 max
|
47 |
-
|
48 |
-
visualizations = []
|
49 |
-
for i, block in enumerate(code_blocks, 1):
|
50 |
-
try:
|
51 |
-
# HD configuration
|
52 |
-
plt.figure(figsize=(16, 9), dpi=120)
|
53 |
-
plt.style.use('seaborn')
|
54 |
-
|
55 |
-
# Clean and execute code
|
56 |
-
cleaned_code = '\n'.join([line.strip() for line in block.split('\n')[1:] if line.strip()])
|
57 |
-
exec(cleaned_code, {'df': df, 'plt': plt})
|
58 |
-
plt.title(f"Visualization {i}", fontsize=14)
|
59 |
-
plt.tight_layout()
|
60 |
-
|
61 |
-
# Save HD image
|
62 |
-
buf = io.BytesIO()
|
63 |
-
plt.savefig(buf, format='png', dpi=120, bbox_inches='tight')
|
64 |
-
plt.close()
|
65 |
-
buf.seek(0)
|
66 |
-
visualizations.append(Image.open(buf))
|
67 |
-
except Exception as e:
|
68 |
-
print(f"Visualization {i} failed: {str(e)}")
|
69 |
-
visualizations.append(Image.new('RGB', (1920, 1080), color=(73, 109, 137)))
|
70 |
-
|
71 |
-
# Ensure exactly 3 outputs with fallback
|
72 |
-
while len(visualizations) < 3:
|
73 |
-
visualizations.append(Image.new('RGB', (1920, 1080), color=(73, 109, 137)))
|
74 |
-
|
75 |
-
return visualizations[:3]
|
76 |
-
|
77 |
-
# Gradio interface with HD layout
|
78 |
-
with gr.Blocks(theme=gr.themes.Default(spacing_size="xl")) as demo:
|
79 |
gr.Markdown("# **HD Data Visualizer** πβ¨")
|
80 |
|
81 |
with gr.Row():
|
82 |
api_key = gr.Textbox(label="π Gemini API Key", type="password")
|
83 |
file = gr.File(label="π Upload Dataset", file_types=[".csv", ".xlsx"])
|
84 |
|
85 |
-
instructions = gr.Textbox(label="π‘ Custom Instructions (optional)",
|
|
|
86 |
submit = gr.Button("π Generate Visualizations", variant="primary")
|
87 |
|
88 |
with gr.Row():
|
|
|
6 |
from PIL import Image
|
7 |
|
8 |
def process_file(api_key, file, instructions):
|
9 |
+
# ... [keep the existing process_file function identical] ...
|
|
|
10 |
|
11 |
+
# Fixed Gradio interface with compatible theme settings
|
12 |
+
with gr.Blocks(theme=gr.themes.Default(spacing_size="lg")) as demo: # Changed from "xl" to "lg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
gr.Markdown("# **HD Data Visualizer** πβ¨")
|
14 |
|
15 |
with gr.Row():
|
16 |
api_key = gr.Textbox(label="π Gemini API Key", type="password")
|
17 |
file = gr.File(label="π Upload Dataset", file_types=[".csv", ".xlsx"])
|
18 |
|
19 |
+
instructions = gr.Textbox(label="π‘ Custom Instructions (optional)",
|
20 |
+
placeholder="E.g.: Focus on time series patterns...")
|
21 |
submit = gr.Button("π Generate Visualizations", variant="primary")
|
22 |
|
23 |
with gr.Row():
|