Update app.py
Browse files
app.py
CHANGED
|
@@ -2,52 +2,79 @@ 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 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
-
import ast
|
| 8 |
-
import re
|
| 9 |
import traceback
|
| 10 |
|
| 11 |
def process_file(api_key, file, instructions):
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
def generate_error_image(message):
|
| 24 |
"""Create error indication image with message"""
|
| 25 |
try:
|
| 26 |
-
img = Image.new('RGB', (
|
| 27 |
draw = ImageDraw.Draw(img)
|
| 28 |
-
|
| 29 |
-
# Use default font
|
| 30 |
font = ImageFont.load_default()
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return img
|
| 41 |
except Exception as e:
|
| 42 |
-
|
| 43 |
-
return Image.new('RGB', (1920, 1080), color=(255, 255, 255))
|
| 44 |
|
| 45 |
# Gradio interface
|
| 46 |
with gr.Blocks(theme=gr.themes.Default(spacing_size="lg")) as demo:
|
| 47 |
gr.Markdown("# Professional Data Visualizer")
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
-
api_key = gr.Textbox(label="
|
| 51 |
file = gr.File(label="Upload Data File", file_types=[".csv", ".xlsx"])
|
| 52 |
|
| 53 |
instructions = gr.Textbox(label="Visualization Instructions")
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import io
|
|
|
|
| 5 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
| 6 |
import traceback
|
| 7 |
|
| 8 |
def process_file(api_key, file, instructions):
|
| 9 |
+
try:
|
| 10 |
+
# Read uploaded file
|
| 11 |
+
if file.name.endswith('.csv'):
|
| 12 |
+
df = pd.read_csv(file.name)
|
| 13 |
+
elif file.name.endswith('.xlsx'):
|
| 14 |
+
df = pd.read_excel(file.name)
|
| 15 |
+
else:
|
| 16 |
+
raise ValueError("Unsupported file format")
|
| 17 |
+
|
| 18 |
+
# Generate sample visualizations (replace with actual logic)
|
| 19 |
+
fig1, ax1 = plt.subplots()
|
| 20 |
+
df.plot(kind='bar', ax=ax1)
|
| 21 |
+
ax1.set_title("Sample Bar Chart")
|
| 22 |
+
|
| 23 |
+
fig2, ax2 = plt.subplots()
|
| 24 |
+
df.plot(kind='line', ax=ax2)
|
| 25 |
+
ax2.set_title("Sample Line Chart")
|
| 26 |
+
|
| 27 |
+
fig3, ax3 = plt.subplots()
|
| 28 |
+
df.plot(kind='hist', ax=ax3)
|
| 29 |
+
ax3.set_title("Sample Histogram")
|
| 30 |
+
|
| 31 |
+
# Convert plots to PIL Images
|
| 32 |
+
def fig_to_image(fig):
|
| 33 |
+
buf = io.BytesIO()
|
| 34 |
+
fig.savefig(buf, format='png')
|
| 35 |
+
buf.seek(0)
|
| 36 |
+
return Image.open(buf)
|
| 37 |
|
| 38 |
+
return [
|
| 39 |
+
fig_to_image(fig1),
|
| 40 |
+
fig_to_image(fig2),
|
| 41 |
+
fig_to_image(fig3)
|
| 42 |
+
]
|
| 43 |
|
| 44 |
+
except Exception as e:
|
| 45 |
+
error_message = f"{str(e)}\n{traceback.format_exc()}"
|
| 46 |
+
return [generate_error_image(error_message)] * 3
|
| 47 |
|
| 48 |
def generate_error_image(message):
|
| 49 |
"""Create error indication image with message"""
|
| 50 |
try:
|
| 51 |
+
img = Image.new('RGB', (800, 400), color=(255, 255, 255))
|
| 52 |
draw = ImageDraw.Draw(img)
|
|
|
|
|
|
|
| 53 |
font = ImageFont.load_default()
|
| 54 |
|
| 55 |
+
# Wrap text
|
| 56 |
+
lines = []
|
| 57 |
+
for line in message.split('\n'):
|
| 58 |
+
if len(line) > 80:
|
| 59 |
+
lines.extend([line[i:i+80] for i in range(0, len(line), 80)])
|
| 60 |
+
else:
|
| 61 |
+
lines.append(line)
|
| 62 |
+
|
| 63 |
+
y_text = 10
|
| 64 |
+
for line in lines[:20]: # Limit to 20 lines
|
| 65 |
+
draw.text((10, y_text), line, font=font, fill=(255, 0, 0))
|
| 66 |
+
y_text += 15
|
| 67 |
+
|
| 68 |
return img
|
| 69 |
except Exception as e:
|
| 70 |
+
return Image.new('RGB', (800, 400), color=(255, 255, 255))
|
|
|
|
| 71 |
|
| 72 |
# Gradio interface
|
| 73 |
with gr.Blocks(theme=gr.themes.Default(spacing_size="lg")) as demo:
|
| 74 |
gr.Markdown("# Professional Data Visualizer")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
+
api_key = gr.Textbox(label="API Key", type="password")
|
| 78 |
file = gr.File(label="Upload Data File", file_types=[".csv", ".xlsx"])
|
| 79 |
|
| 80 |
instructions = gr.Textbox(label="Visualization Instructions")
|