Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,12 @@ app = FastAPI()
|
|
| 21 |
vqa_pipeline = pipeline("image-to-text", model="Salesforce/blip-vqa-base")
|
| 22 |
code_generator = pipeline("text-generation", model="openai-community/gpt2-medium")
|
| 23 |
table_analyzer = pipeline("table-question-answering", model="google/tapas-large-finetuned-wtq")
|
| 24 |
-
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large") # β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# β
Functions for Document & Image QA
|
| 27 |
def extract_text_from_pdf(pdf_file):
|
|
@@ -74,18 +79,21 @@ def answer_question_from_document(file, question):
|
|
| 74 |
if not text:
|
| 75 |
return "No text extracted from the document."
|
| 76 |
|
| 77 |
-
# β
|
| 78 |
-
|
|
|
|
| 79 |
response = qa_pipeline(input_text)
|
| 80 |
|
| 81 |
-
return response[0]["generated_text"] # β
|
| 82 |
|
| 83 |
def answer_question_from_image(image, question):
|
| 84 |
image_text = extract_text_from_image(image)
|
| 85 |
if not image_text:
|
| 86 |
return "No text detected in the image."
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
response = qa_pipeline(input_text)
|
| 90 |
|
| 91 |
return response[0]["generated_text"]
|
|
|
|
| 21 |
vqa_pipeline = pipeline("image-to-text", model="Salesforce/blip-vqa-base")
|
| 22 |
code_generator = pipeline("text-generation", model="openai-community/gpt2-medium")
|
| 23 |
table_analyzer = pipeline("table-question-answering", model="google/tapas-large-finetuned-wtq")
|
| 24 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large") # β
FLAN-T5 Fixed
|
| 25 |
+
|
| 26 |
+
# β
Function to truncate text to 450 tokens
|
| 27 |
+
def truncate_text(text, max_tokens=450):
|
| 28 |
+
words = text.split()
|
| 29 |
+
return " ".join(words[:max_tokens]) # β
Keeps only the first 450 words
|
| 30 |
|
| 31 |
# β
Functions for Document & Image QA
|
| 32 |
def extract_text_from_pdf(pdf_file):
|
|
|
|
| 79 |
if not text:
|
| 80 |
return "No text extracted from the document."
|
| 81 |
|
| 82 |
+
truncated_text = truncate_text(text) # β
Prevents token limit error
|
| 83 |
+
|
| 84 |
+
input_text = f"Question: {question} Context: {truncated_text}" # β
Proper FLAN-T5 format
|
| 85 |
response = qa_pipeline(input_text)
|
| 86 |
|
| 87 |
+
return response[0]["generated_text"] # β
Returns the correct output
|
| 88 |
|
| 89 |
def answer_question_from_image(image, question):
|
| 90 |
image_text = extract_text_from_image(image)
|
| 91 |
if not image_text:
|
| 92 |
return "No text detected in the image."
|
| 93 |
|
| 94 |
+
truncated_text = truncate_text(image_text) # β
Prevents token limit error
|
| 95 |
+
|
| 96 |
+
input_text = f"Question: {question} Context: {truncated_text}"
|
| 97 |
response = qa_pipeline(input_text)
|
| 98 |
|
| 99 |
return response[0]["generated_text"]
|