Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import pandas as pd
|
2 |
import matplotlib.pyplot as plt
|
3 |
import io
|
@@ -6,12 +7,6 @@ from PIL import Image, ImageDraw
|
|
6 |
import google.generativeai as genai
|
7 |
import traceback
|
8 |
import os
|
9 |
-
from pywebio import start_server
|
10 |
-
from pywebio.input import file_upload, input
|
11 |
-
from pywebio.output import put_text, put_image, put_row, put_column, use_scope, put_buttons
|
12 |
-
from pywebio.session import run_js, set_env
|
13 |
-
import base64
|
14 |
-
import threading
|
15 |
|
16 |
def process_file(file, instructions):
|
17 |
try:
|
@@ -21,11 +16,8 @@ def process_file(file, instructions):
|
|
21 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
22 |
|
23 |
# Read uploaded file
|
24 |
-
|
25 |
-
if
|
26 |
-
df = pd.read_csv(io.BytesIO(content))
|
27 |
-
else:
|
28 |
-
df = pd.read_excel(io.BytesIO(content))
|
29 |
|
30 |
# Generate visualization code
|
31 |
response = model.generate_content(f"""
|
@@ -40,7 +32,6 @@ def process_file(file, instructions):
|
|
40 |
2. Determine appropriate data aggregation (e.g., top 5 categories, yearly averages)
|
41 |
3. Select relevant columns for x-axis, y-axis, and any additional dimensions (color, size)
|
42 |
4. Provide a clear, concise title that explains the insight
|
43 |
-
|
44 |
Consider data density and choose visualizations that simplify and clarify the information.
|
45 |
Limit the number of data points displayed to ensure readability (e.g., top 5, top 10, yearly).
|
46 |
|
@@ -123,42 +114,22 @@ def process_file(file, instructions):
|
|
123 |
draw.text((10, 10), error_message, fill=(255, 0, 0))
|
124 |
return [error_image] * 3
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
put_text("# Data Analysis Dashboard")
|
129 |
-
|
130 |
-
with use_scope('form'):
|
131 |
-
put_row([
|
132 |
-
put_column([
|
133 |
-
file_upload("Upload Dataset", accept=[".csv", ".xlsx"], name="file"),
|
134 |
-
input("Analysis Instructions", type="text", placeholder="Describe the analysis you want...", name="instructions"),
|
135 |
-
put_buttons(['Generate Insights'], onclick=[lambda: generate_insights()])
|
136 |
-
])
|
137 |
-
])
|
138 |
-
|
139 |
-
with use_scope('output'):
|
140 |
-
for i in range(3):
|
141 |
-
put_scope(f'visualization_{i+1}')
|
142 |
-
|
143 |
-
def generate_insights():
|
144 |
-
file = file_upload.files.get('file')
|
145 |
-
instructions = input.inputs.get('instructions')
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
|
151 |
-
|
152 |
|
153 |
-
for i
|
154 |
-
buffered = io.BytesIO()
|
155 |
-
img.save(buffered, format="PNG")
|
156 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
157 |
-
with use_scope(f'visualization_{i+1}', clear=True):
|
158 |
-
put_image(img_str, width='100%')
|
159 |
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
162 |
|
163 |
-
if __name__ ==
|
164 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import matplotlib.pyplot as plt
|
4 |
import io
|
|
|
7 |
import google.generativeai as genai
|
8 |
import traceback
|
9 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def process_file(file, instructions):
|
12 |
try:
|
|
|
16 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
17 |
|
18 |
# Read uploaded file
|
19 |
+
file_path = file.name
|
20 |
+
df = pd.read_csv(file_path) if file_path.endswith('.csv') else pd.read_excel(file_path)
|
|
|
|
|
|
|
21 |
|
22 |
# Generate visualization code
|
23 |
response = model.generate_content(f"""
|
|
|
32 |
2. Determine appropriate data aggregation (e.g., top 5 categories, yearly averages)
|
33 |
3. Select relevant columns for x-axis, y-axis, and any additional dimensions (color, size)
|
34 |
4. Provide a clear, concise title that explains the insight
|
|
|
35 |
Consider data density and choose visualizations that simplify and clarify the information.
|
36 |
Limit the number of data points displayed to ensure readability (e.g., top 5, top 10, yearly).
|
37 |
|
|
|
114 |
draw.text((10, 10), error_message, fill=(255, 0, 0))
|
115 |
return [error_image] * 3
|
116 |
|
117 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
118 |
+
gr.Markdown("# Data Analysis Dashboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
with gr.Row():
|
121 |
+
file = gr.File(label="Upload Dataset", file_types=[".csv", ".xlsx"])
|
122 |
+
instructions = gr.Textbox(label="Analysis Instructions", placeholder="Describe the analysis you want...")
|
123 |
|
124 |
+
submit = gr.Button("Generate Insights", variant="primary")
|
125 |
|
126 |
+
output_images = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
submit.click(
|
129 |
+
process_file,
|
130 |
+
inputs=[file, instructions],
|
131 |
+
outputs=output_images
|
132 |
+
)
|
133 |
|
134 |
+
if __name__ == "__main__":
|
135 |
+
demo.launch()
|