Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
-
def
|
7 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
from fastapi import FastAPI
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# ✅ Load Lightweight Hugging Face Models
|
10 |
+
code_generator = pipeline("text-generation", model="Salesforce/codegen-350M-multi")
|
11 |
+
table_analyzer = pipeline("table-question-answering", model="google/tapas-small-finetuned-wtq")
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
15 |
+
def generate_visualization(excel_file, user_request):
|
16 |
+
# Load Excel file into DataFrame
|
17 |
+
df = pd.read_excel(excel_file)
|
18 |
+
|
19 |
+
# Convert DataFrame to JSON (for TAPAS input)
|
20 |
+
table_data = df.to_dict(orient="records")
|
21 |
+
|
22 |
+
# Analyze table with TAPAS
|
23 |
+
table_answer = table_analyzer(question=user_request, table=table_data)
|
24 |
+
|
25 |
+
# Generate Python visualization code with CodeGen
|
26 |
+
prompt = f"Generate Python code using Matplotlib and Seaborn to visualize: {user_request} from this data:\n{df.head()}"
|
27 |
+
code_response = code_generator(prompt, max_new_tokens=100, do_sample=True)[0]["generated_text"]
|
28 |
+
|
29 |
+
# Extract relevant Python code (basic cleaning)
|
30 |
+
code_lines = code_response.split("\n")
|
31 |
+
python_code = "\n".join([line for line in code_lines if "import" in line or "plt." in line or "sns." in line])
|
32 |
+
|
33 |
+
# Execute generated code safely
|
34 |
+
exec_globals = {"df": df, "plt": plt, "sns": sns}
|
35 |
+
exec(python_code, exec_globals)
|
36 |
+
|
37 |
+
# Save and return plot
|
38 |
+
plt.savefig("output.png")
|
39 |
+
plt.close()
|
40 |
+
|
41 |
+
return "output.png", python_code, table_answer
|
42 |
+
|
43 |
+
# ✅ Gradio Interface
|
44 |
+
gradio_ui = gr.Interface(
|
45 |
+
fn=generate_visualization,
|
46 |
+
inputs=[gr.File(label="Upload Excel File"), gr.Textbox(label="Enter Visualization Request")],
|
47 |
+
outputs=[gr.Image(label="Generated Visualization"), gr.Code(label="Generated Python Code"), gr.Textbox(label="Table Insights")],
|
48 |
+
title="Lightweight AI-Powered Data Visualization"
|
49 |
+
)
|
50 |
+
|
51 |
+
# ✅ Mount Gradio to FastAPI
|
52 |
@app.get("/")
|
53 |
+
def home():
|
54 |
+
return {"message": "Welcome to AI-Powered Data Visualization"}
|
55 |
+
|
56 |
+
app = gr.mount_gradio_app(app, gradio_ui, path="/visualization")
|
57 |
+
|
58 |
+
|
59 |
+
|