bluenevus commited on
Commit
7a74363
·
verified ·
1 Parent(s): 904e6a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -17
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import gradio as gr
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import io
@@ -7,6 +6,12 @@ from PIL import Image, ImageDraw
7
  import google.generativeai as genai
8
  import traceback
9
  import os
 
 
 
 
 
 
10
 
11
  def process_file(file, instructions):
12
  try:
@@ -16,8 +21,11 @@ def process_file(file, instructions):
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"""
@@ -115,22 +123,41 @@ def process_file(file, instructions):
115
  draw.text((10, 10), error_message, fill=(255, 0, 0))
116
  return [error_image] * 3
117
 
118
- with gr.Blocks(theme=gr.themes.Default()) as demo:
119
- gr.Markdown("# Data Analysis Dashboard")
120
 
121
- with gr.Row():
122
- file = gr.File(label="Upload Dataset", file_types=[".csv", ".xlsx"])
123
- instructions = gr.Textbox(label="Analysis Instructions", placeholder="Describe the analysis you want...")
 
 
 
 
 
124
 
125
- submit = gr.Button("Generate Insights", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
- output_images = [gr.Image(label=f"Visualization {i+1}") for i in range(3)]
 
 
 
 
 
128
 
129
- submit.click(
130
- process_file,
131
- inputs=[file, instructions],
132
- outputs=output_images
133
- )
134
 
135
- if __name__ == "__main__":
136
- demo.launch()
 
 
1
  import pandas as pd
2
  import matplotlib.pyplot as plt
3
  import io
 
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, put_buttons, use_scope
12
+ from pywebio.session import run_js
13
+ import base64
14
+ import threading
15
 
16
  def process_file(file, instructions):
17
  try:
 
21
  model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
22
 
23
  # Read uploaded file
24
+ content = file['content']
25
+ if file['filename'].endswith('.csv'):
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"""
 
123
  draw.text((10, 10), error_message, fill=(255, 0, 0))
124
  return [error_image] * 3
125
 
126
+ def data_analysis_dashboard():
127
+ put_text("# Data Analysis Dashboard")
128
 
129
+ with use_scope('form'):
130
+ put_row([
131
+ put_column([
132
+ file_upload("Upload Dataset", accept=[".csv", ".xlsx"], name="file"),
133
+ input("Analysis Instructions", type="text", placeholder="Describe the analysis you want...", name="instructions"),
134
+ put_buttons(['Generate Insights'], onclick=[lambda: generate_insights()])
135
+ ])
136
+ ])
137
 
138
+ with use_scope('output'):
139
+ for i in range(3):
140
+ put_image(name=f'visualization_{i+1}')
141
+
142
+ def generate_insights():
143
+ file = file_upload.files.get('file')
144
+ instructions = input.inputs.get('instructions')
145
+
146
+ if not file or not instructions:
147
+ put_text("Please upload a file and provide instructions.")
148
+ return
149
+
150
+ images = process_file(file, instructions)
151
 
152
+ for i, img in enumerate(images):
153
+ buffered = io.BytesIO()
154
+ img.save(buffered, format="PNG")
155
+ img_str = base64.b64encode(buffered.getvalue()).decode()
156
+ with use_scope(f'visualization_{i+1}', clear=True):
157
+ put_image(img_str, width='100%')
158
 
159
+ def main():
160
+ data_analysis_dashboard()
 
 
 
161
 
162
+ if __name__ == '__main__':
163
+ start_server(main, host='0.0.0.0', port=7860, debug=True, cdn=False, auto_open_webbrowser=True)