Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,37 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
import pdfplumber
|
|
|
|
|
|
|
3 |
import docx
|
4 |
import openpyxl
|
5 |
from pptx import Presentation
|
6 |
-
import easyocr
|
7 |
from transformers import pipeline
|
8 |
import gradio as gr
|
|
|
|
|
|
|
9 |
from fastapi.responses import RedirectResponse
|
|
|
10 |
|
11 |
-
# Initialize FastAPI
|
12 |
app = FastAPI()
|
13 |
|
14 |
-
# Load AI
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
18 |
def truncate_text(text, max_tokens=450):
|
19 |
words = text.split()
|
20 |
-
return " ".join(words[:max_tokens])
|
21 |
|
22 |
-
# Functions
|
23 |
def extract_text_from_pdf(pdf_file):
|
24 |
text = ""
|
25 |
with pdfplumber.open(pdf_file) as pdf:
|
@@ -53,7 +65,6 @@ def extract_text_from_image(image_file):
|
|
53 |
result = reader.readtext(image_file)
|
54 |
return " ".join([res[1] for res in result])
|
55 |
|
56 |
-
# Function to answer questions based on document content
|
57 |
def answer_question_from_document(file, question):
|
58 |
file_ext = file.name.split(".")[-1].lower()
|
59 |
|
@@ -70,26 +81,27 @@ def answer_question_from_document(file, question):
|
|
70 |
|
71 |
if not text:
|
72 |
return "No text extracted from the document."
|
73 |
-
|
74 |
-
truncated_text = truncate_text(text)
|
75 |
-
|
|
|
76 |
response = qa_pipeline(input_text)
|
77 |
-
|
78 |
-
return response[0]["generated_text"]
|
79 |
|
80 |
-
#
|
|
|
81 |
def answer_question_from_image(image, question):
|
82 |
image_text = extract_text_from_image(image)
|
83 |
if not image_text:
|
84 |
return "No text detected in the image."
|
85 |
-
|
86 |
-
truncated_text = truncate_text(image_text)
|
|
|
87 |
input_text = f"Question: {question} Context: {truncated_text}"
|
88 |
response = qa_pipeline(input_text)
|
89 |
-
|
90 |
return response[0]["generated_text"]
|
91 |
|
92 |
-
# Gradio UI for Document & Image QA
|
93 |
doc_interface = gr.Interface(
|
94 |
fn=answer_question_from_document,
|
95 |
inputs=[gr.File(label="Upload Document"), gr.Textbox(label="Ask a Question")],
|
@@ -104,10 +116,63 @@ img_interface = gr.Interface(
|
|
104 |
title="AI Image Question Answering"
|
105 |
)
|
106 |
|
107 |
-
#
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
app = gr.mount_gradio_app(app, demo, path="/")
|
110 |
|
111 |
@app.get("/")
|
112 |
def home():
|
113 |
-
return RedirectResponse(url="/")
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
import pdfplumber
|
3 |
+
import pytesseract
|
4 |
+
from PIL import Image
|
5 |
+
import easyocr
|
6 |
import docx
|
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 |
+
from transformers import pipeline
|
22 |
+
|
23 |
+
qa_pipeline = pipeline("text2text-generation",model="google/flan-t5-large",tokenizer="google/flan-t5-large",use_fast=True,device=0)
|
24 |
+
table_analyzer = pipeline("table-question-answering",model="google/tapas-large-finetuned-wtq",tokenizer="google/tapas-large-finetuned-wtq",use_fast=True,device=0)
|
25 |
+
code_generator = pipeline("text-generation",model="openai-community/gpt2-medium",tokenizer="openai-community/gpt2-medium",use_fast=True,device=0)
|
26 |
+
vqa_pipeline = pipeline("image-to-text",model="Salesforce/blip-vqa-base",device=0 )
|
27 |
|
28 |
+
|
29 |
+
# β
Function to truncate text to 450 tokens
|
30 |
def truncate_text(text, max_tokens=450):
|
31 |
words = text.split()
|
32 |
+
return " ".join(words[:max_tokens]) # β
Keeps only the first 450 words
|
33 |
|
34 |
+
# β
Functions for Document & Image QA
|
35 |
def extract_text_from_pdf(pdf_file):
|
36 |
text = ""
|
37 |
with pdfplumber.open(pdf_file) as pdf:
|
|
|
65 |
result = reader.readtext(image_file)
|
66 |
return " ".join([res[1] for res in result])
|
67 |
|
|
|
68 |
def answer_question_from_document(file, question):
|
69 |
file_ext = file.name.split(".")[-1].lower()
|
70 |
|
|
|
81 |
|
82 |
if not text:
|
83 |
return "No text extracted from the document."
|
84 |
+
|
85 |
+
truncated_text = truncate_text(text) # β
Prevents token limit error
|
86 |
+
|
87 |
+
input_text = f"Question: {question} Context: {truncated_text}" # β
Proper FLAN-T5 format
|
88 |
response = qa_pipeline(input_text)
|
|
|
|
|
89 |
|
90 |
+
return response[0]["generated_text"] # β
Returns the correct output
|
91 |
+
|
92 |
def answer_question_from_image(image, question):
|
93 |
image_text = extract_text_from_image(image)
|
94 |
if not image_text:
|
95 |
return "No text detected in the image."
|
96 |
+
|
97 |
+
truncated_text = truncate_text(image_text) # β
Prevents token limit error
|
98 |
+
|
99 |
input_text = f"Question: {question} Context: {truncated_text}"
|
100 |
response = qa_pipeline(input_text)
|
101 |
+
|
102 |
return response[0]["generated_text"]
|
103 |
|
104 |
+
# β
Gradio UI for Document & Image QA
|
105 |
doc_interface = gr.Interface(
|
106 |
fn=answer_question_from_document,
|
107 |
inputs=[gr.File(label="Upload Document"), gr.Textbox(label="Ask a Question")],
|
|
|
116 |
title="AI Image Question Answering"
|
117 |
)
|
118 |
|
119 |
+
# β
Data Visualization Function
|
120 |
+
def generate_visualization(excel_file, viz_type, user_request):
|
121 |
+
try:
|
122 |
+
df = pd.read_excel(excel_file)
|
123 |
+
df = df.astype(str).fillna("")
|
124 |
+
|
125 |
+
table_input = {
|
126 |
+
"table": df.to_dict(orient="records"),
|
127 |
+
"query": user_request.strip() if isinstance(user_request, str) else "What is the summary?"
|
128 |
+
}
|
129 |
+
|
130 |
+
table_answer = table_analyzer(**table_input)
|
131 |
+
|
132 |
+
prompt = (
|
133 |
+
f"Given a dataset with columns {list(df.columns)}, generate Python code using Matplotlib and Seaborn "
|
134 |
+
f"to create a {viz_type.lower()} based on: {user_request}. Only return valid Python code, no explanations."
|
135 |
+
)
|
136 |
+
code_response = code_generator(prompt, max_new_tokens=150, do_sample=True)
|
137 |
+
|
138 |
+
if isinstance(code_response, list) and "generated_text" in code_response[0]:
|
139 |
+
generated_code = code_response[0]["generated_text"]
|
140 |
+
else:
|
141 |
+
generated_code = "Error: Model did not return valid code."
|
142 |
+
|
143 |
+
try:
|
144 |
+
exec_globals = {"plt": plt, "sns": sns, "pd": pd, "df": df, "io": io}
|
145 |
+
exec(generated_code, exec_globals)
|
146 |
+
|
147 |
+
fig = plt.gcf()
|
148 |
+
img_buf = io.BytesIO()
|
149 |
+
fig.savefig(img_buf, format='png')
|
150 |
+
img_buf.seek(0)
|
151 |
+
plt.close(fig)
|
152 |
+
except Exception as e:
|
153 |
+
return generated_code, f"Error in executing visualization: {str(e)}"
|
154 |
+
|
155 |
+
return generated_code, img_buf
|
156 |
+
|
157 |
+
except Exception as e:
|
158 |
+
return f"Error: {str(e)}", "Failed to analyze table."
|
159 |
+
|
160 |
+
# β
Gradio UI for Data Visualization
|
161 |
+
viz_interface = gr.Interface(
|
162 |
+
fn=generate_visualization,
|
163 |
+
inputs=[
|
164 |
+
gr.File(label="Upload Excel File"),
|
165 |
+
gr.Radio(["Bar Chart", "Line Chart", "Scatter Plot", "Histogram"], label="Choose Visualization Type"),
|
166 |
+
gr.Textbox(label="Enter Visualization Request")
|
167 |
+
],
|
168 |
+
outputs=[gr.Code(label="Generated Python Code"), gr.Image(label="Visualization Output")],
|
169 |
+
title="AI-Powered Data Visualization"
|
170 |
+
)
|
171 |
+
|
172 |
+
# β
Mount Gradio Interfaces
|
173 |
+
demo = gr.TabbedInterface([doc_interface, img_interface, viz_interface], ["Document QA", "Image QA"])
|
174 |
app = gr.mount_gradio_app(app, demo, path="/")
|
175 |
|
176 |
@app.get("/")
|
177 |
def home():
|
178 |
+
return RedirectResponse(url="/")
|