ikraamkb commited on
Commit
c724805
Β·
verified Β·
1 Parent(s): f57a980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
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") # βœ… FIXED
 
 
 
 
 
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
- # βœ… FLAN-T5 expects input in a specific format
78
- input_text = f"Question: {question} Context: {text}"
 
79
  response = qa_pipeline(input_text)
80
 
81
- return response[0]["generated_text"] # βœ… FIXED OUTPUT EXTRACTION
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
- input_text = f"Question: {question} Context: {image_text}"
 
 
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"]