bluenevus's picture
Update app.py
601022d verified
raw
history blame
3.94 kB
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import io
import json
from PIL import Image, ImageDraw
import google.generativeai as genai
import traceback
def process_file(file, instructions, api_key):
try:
# Initialize Gemini
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
# Read uploaded file
file_path = file.name
df = pd.read_csv(file_path) if file_path.endswith('.csv') else pd.read_excel(file_path)
# Generate visualization code using Gemini
prompt = f"""
Analyze the following dataset and instructions:
Data columns: {list(df.columns)}
Instructions: {instructions}
Based on this, create 3 appropriate visualizations. For each visualization, provide:
1. A title
2. The most suitable plot type (choose from: bar, line, scatter, hist)
3. The column to use for the x-axis
4. The column to use for the y-axis (use None for histograms)
5. A brief explanation of why this visualization is appropriate
Return your response as a JSON string in this format:
[
{{"title": "...", "plot_type": "...", "x": "...", "y": "...", "explanation": "..."}},
{{"title": "...", "plot_type": "...", "x": "...", "y": "...", "explanation": "..."}},
{{"title": "...", "plot_type": "...", "x": "...", "y": "...", "explanation": "..."}}
]
"""
response = model.generate_content(prompt)
plots = json.loads(response.text)
# Generate visualizations
images = []
for plot in plots:
fig, ax = plt.subplots(figsize=(10, 6))
title, plot_type, x, y = plot['title'], plot['plot_type'], plot['x'], plot['y']
if plot_type == 'bar':
df.plot(kind='bar', x=x, y=y, ax=ax)
elif plot_type == 'line':
df.plot(kind='line', x=x, y=y, ax=ax)
elif plot_type == 'scatter':
df.plot(kind='scatter', x=x, y=y, ax=ax)
elif plot_type == 'hist':
df[x].hist(ax=ax)
ax.set_title(title)
ax.set_xlabel(x)
ax.set_ylabel(y if y else 'Frequency')
plt.tight_layout()
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
img = Image.open(buf)
images.append((img, plot['explanation']))
plt.close(fig)
return images if len(images) == 3 else images + [(Image.new('RGB', (800, 600), (255,255,255)), "")]*(3-len(images))
except Exception as e:
error_message = f"Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
print(error_message) # Print to console for debugging
error_image = Image.new('RGB', (800, 400), (255, 255, 255))
draw = ImageDraw.Draw(error_image)
draw.text((10, 10), error_message, fill=(255, 0, 0))
return [(error_image, "An error occurred")] * 3
with gr.Blocks(theme=gr.themes.Default()) as demo:
gr.Markdown("# Data Analysis Dashboard")
with gr.Row():
file = gr.File(label="Upload Dataset", file_types=[".csv", ".xlsx"])
instructions = gr.Textbox(label="Analysis Instructions", placeholder="Describe the analysis you want...")
api_key = gr.Textbox(label="Gemini API Key", type="password")
submit = gr.Button("Generate Insights", variant="primary")
output_images = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
output_texts = [gr.Textbox(label=f"Explanation {i+1}") for i in range(3)]
submit.click(
process_file,
inputs=[file, instructions, api_key],
outputs=output_images + output_texts
)
if __name__ == "__main__":
demo.launch()