Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
-
from typing import List
|
3 |
import pdfplumber
|
4 |
import pytesseract
|
5 |
from PIL import Image
|
@@ -8,14 +7,23 @@ import docx
|
|
8 |
import openpyxl
|
9 |
from pptx import Presentation
|
10 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
11 |
import io
|
12 |
|
|
|
13 |
app = FastAPI()
|
14 |
|
15 |
-
# Load
|
16 |
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
17 |
-
vqa_pipeline = pipeline("image-to-text", model="Salesforce/blip-vqa-base")
|
|
|
|
|
18 |
|
|
|
19 |
def extract_text_from_pdf(pdf_file):
|
20 |
text = ""
|
21 |
with pdfplumber.open(pdf_file) as pdf:
|
@@ -49,35 +57,106 @@ def extract_text_from_image(image_file):
|
|
49 |
result = reader.readtext(image_file)
|
50 |
return " ".join([res[1] for res in result])
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
file_ext = file.filename.split(".")[-1].lower()
|
55 |
|
56 |
if file_ext == "pdf":
|
57 |
-
text = extract_text_from_pdf(
|
58 |
elif file_ext == "docx":
|
59 |
-
text = extract_text_from_docx(
|
60 |
elif file_ext == "pptx":
|
61 |
-
text = extract_text_from_pptx(
|
62 |
elif file_ext == "xlsx":
|
63 |
-
text = extract_text_from_excel(
|
64 |
else:
|
65 |
-
return
|
66 |
|
67 |
if not text:
|
68 |
-
return
|
69 |
|
70 |
response = qa_pipeline(question=question, context=text)
|
71 |
-
return
|
72 |
|
73 |
-
|
74 |
-
async def qa_image(file: UploadFile = File(...), question: str = Form(...)):
|
75 |
-
image = Image.open(io.BytesIO(await file.read()))
|
76 |
image_text = extract_text_from_image(image)
|
77 |
-
|
78 |
if not image_text:
|
79 |
-
return
|
80 |
|
81 |
response = qa_pipeline(question=question, context=image_text)
|
82 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
|
|
2 |
import pdfplumber
|
3 |
import pytesseract
|
4 |
from PIL import Image
|
|
|
7 |
import openpyxl
|
8 |
from pptx import Presentation
|
9 |
from transformers import pipeline
|
10 |
+
import gradio as gr
|
11 |
+
import pandas as pd
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
import seaborn as sns
|
14 |
+
from fastapi.responses import RedirectResponse
|
15 |
import io
|
16 |
|
17 |
+
# β
Initialize FastAPI
|
18 |
app = FastAPI()
|
19 |
|
20 |
+
# β
Load AI Models
|
21 |
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
22 |
+
vqa_pipeline = pipeline("image-to-text", model="Salesforce/blip-vqa-base")
|
23 |
+
code_generator = pipeline("text-generation", model="openai-community/gpt2-medium")
|
24 |
+
table_analyzer = pipeline("table-question-answering", model="google/tapas-large-finetuned-wtq")
|
25 |
|
26 |
+
# β
Functions for Document & Image QA
|
27 |
def extract_text_from_pdf(pdf_file):
|
28 |
text = ""
|
29 |
with pdfplumber.open(pdf_file) as pdf:
|
|
|
57 |
result = reader.readtext(image_file)
|
58 |
return " ".join([res[1] for res in result])
|
59 |
|
60 |
+
def answer_question_from_document(file, question):
|
61 |
+
file_ext = file.name.split(".")[-1].lower()
|
|
|
62 |
|
63 |
if file_ext == "pdf":
|
64 |
+
text = extract_text_from_pdf(file)
|
65 |
elif file_ext == "docx":
|
66 |
+
text = extract_text_from_docx(file)
|
67 |
elif file_ext == "pptx":
|
68 |
+
text = extract_text_from_pptx(file)
|
69 |
elif file_ext == "xlsx":
|
70 |
+
text = extract_text_from_excel(file)
|
71 |
else:
|
72 |
+
return "Unsupported file format!"
|
73 |
|
74 |
if not text:
|
75 |
+
return "No text extracted from the document."
|
76 |
|
77 |
response = qa_pipeline(question=question, context=text)
|
78 |
+
return response["answer"]
|
79 |
|
80 |
+
def answer_question_from_image(image, question):
|
|
|
|
|
81 |
image_text = extract_text_from_image(image)
|
|
|
82 |
if not image_text:
|
83 |
+
return "No text detected in the image."
|
84 |
|
85 |
response = qa_pipeline(question=question, context=image_text)
|
86 |
+
return response["answer"]
|
87 |
+
|
88 |
+
# β
Gradio UI for Document & Image QA
|
89 |
+
doc_interface = gr.Interface(
|
90 |
+
fn=answer_question_from_document,
|
91 |
+
inputs=[gr.File(label="Upload Document"), gr.Textbox(label="Ask a Question")],
|
92 |
+
outputs="text",
|
93 |
+
title="AI Document Question Answering"
|
94 |
+
)
|
95 |
+
|
96 |
+
img_interface = gr.Interface(
|
97 |
+
fn=answer_question_from_image,
|
98 |
+
inputs=[gr.Image(label="Upload Image"), gr.Textbox(label="Ask a Question")],
|
99 |
+
outputs="text",
|
100 |
+
title="AI Image Question Answering"
|
101 |
+
)
|
102 |
+
|
103 |
+
# β
Data Visualization Function
|
104 |
+
def generate_visualization(excel_file, viz_type, user_request):
|
105 |
+
try:
|
106 |
+
df = pd.read_excel(excel_file)
|
107 |
+
df = df.astype(str).fillna("")
|
108 |
+
|
109 |
+
table_input = {
|
110 |
+
"table": df.to_dict(orient="records"),
|
111 |
+
"query": user_request.strip() if isinstance(user_request, str) else "What is the summary?"
|
112 |
+
}
|
113 |
+
|
114 |
+
table_answer = table_analyzer(**table_input)
|
115 |
+
|
116 |
+
prompt = (
|
117 |
+
f"Given a dataset with columns {list(df.columns)}, generate Python code using Matplotlib and Seaborn "
|
118 |
+
f"to create a {viz_type.lower()} based on: {user_request}. Only return valid Python code, no explanations."
|
119 |
+
)
|
120 |
+
code_response = code_generator(prompt, max_new_tokens=150, do_sample=True)
|
121 |
+
|
122 |
+
if isinstance(code_response, list) and "generated_text" in code_response[0]:
|
123 |
+
generated_code = code_response[0]["generated_text"]
|
124 |
+
else:
|
125 |
+
generated_code = "Error: Model did not return valid code."
|
126 |
+
|
127 |
+
try:
|
128 |
+
exec_globals = {"plt": plt, "sns": sns, "pd": pd, "df": df, "io": io}
|
129 |
+
exec(generated_code, exec_globals)
|
130 |
+
|
131 |
+
fig = plt.gcf()
|
132 |
+
img_buf = io.BytesIO()
|
133 |
+
fig.savefig(img_buf, format='png')
|
134 |
+
img_buf.seek(0)
|
135 |
+
plt.close(fig)
|
136 |
+
except Exception as e:
|
137 |
+
return generated_code, f"Error in executing visualization: {str(e)}"
|
138 |
+
|
139 |
+
return generated_code, img_buf
|
140 |
+
|
141 |
+
except Exception as e:
|
142 |
+
return f"Error: {str(e)}", "Failed to analyze table."
|
143 |
+
|
144 |
+
# β
Gradio UI for Data Visualization
|
145 |
+
viz_interface = gr.Interface(
|
146 |
+
fn=generate_visualization,
|
147 |
+
inputs=[
|
148 |
+
gr.File(label="Upload Excel File"),
|
149 |
+
gr.Radio(["Bar Chart", "Line Chart", "Scatter Plot", "Histogram"], label="Choose Visualization Type"),
|
150 |
+
gr.Textbox(label="Enter Visualization Request")
|
151 |
+
],
|
152 |
+
outputs=[gr.Code(label="Generated Python Code"), gr.Image(label="Visualization Output")],
|
153 |
+
title="AI-Powered Data Visualization"
|
154 |
+
)
|
155 |
+
|
156 |
+
# β
Mount Gradio Interfaces
|
157 |
+
demo = gr.TabbedInterface([doc_interface, img_interface, viz_interface], ["Document QA", "Image QA", "Data Visualization"])
|
158 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
159 |
|
160 |
+
@app.get("/")
|
161 |
+
def home():
|
162 |
+
return RedirectResponse(url="/")
|