Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import io
|
5 |
+
import google.generativeai as genai
|
6 |
+
|
7 |
+
# Function to process the file and generate visualizations
|
8 |
+
def process_file(api_key, file, instructions):
|
9 |
+
# Set up Gemini API
|
10 |
+
genai.configure(api_key=api_key)
|
11 |
+
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
12 |
+
|
13 |
+
# Read the file
|
14 |
+
if file.name.endswith('.csv'):
|
15 |
+
df = pd.read_csv(file.name)
|
16 |
+
else:
|
17 |
+
df = pd.read_excel(file.name)
|
18 |
+
|
19 |
+
# Analyze data and get visualization suggestions from Gemini
|
20 |
+
data_description = df.describe().to_string()
|
21 |
+
prompt = f"Given this data: {data_description}\n"
|
22 |
+
if instructions:
|
23 |
+
prompt += f"And these instructions: {instructions}\n"
|
24 |
+
prompt += "Suggest 3 ways to visualize this data."
|
25 |
+
|
26 |
+
response = model.generate_content(prompt)
|
27 |
+
suggestions = response.text.split('\n')
|
28 |
+
|
29 |
+
# Generate visualizations (placeholder - you'll need to implement actual visualization logic)
|
30 |
+
visualizations = []
|
31 |
+
for i, suggestion in enumerate(suggestions[:3]):
|
32 |
+
plt.figure()
|
33 |
+
plt.title(f"Visualization {i+1}")
|
34 |
+
plt.text(0.5, 0.5, suggestion, ha='center', va='center')
|
35 |
+
buf = io.BytesIO()
|
36 |
+
plt.savefig(buf, format='png')
|
37 |
+
buf.seek(0)
|
38 |
+
visualizations.append(buf)
|
39 |
+
|
40 |
+
return visualizations
|
41 |
+
|
42 |
+
# Gradio interface
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("Data Visualization with Gemini")
|
45 |
+
api_key = gr.Textbox(label="Enter Gemini API Key")
|
46 |
+
file = gr.File(label="Upload Excel or CSV file")
|
47 |
+
instructions = gr.Textbox(label="Optional visualization instructions")
|
48 |
+
submit = gr.Button("Generate Visualizations")
|
49 |
+
progress = gr.Progress()
|
50 |
+
outputs = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
|
51 |
+
|
52 |
+
submit.click(
|
53 |
+
fn=process_file,
|
54 |
+
inputs=[api_key, file, instructions],
|
55 |
+
outputs=outputs
|
56 |
+
)
|
57 |
+
|
58 |
+
demo.launch()
|