ikraamkb commited on
Commit
bdeddcb
·
verified ·
1 Parent(s): 47942ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -27
app.py CHANGED
@@ -70,7 +70,7 @@ from fastapi.responses import RedirectResponse, FileResponse, JSONResponse
70
  import os
71
  import shutil
72
  from PIL import Image
73
- from transformers import ViltProcessor, ViltForQuestionAnswering, AutoTokenizer, AutoModelForCausalLM
74
  from gtts import gTTS
75
  import torch
76
  import tempfile
@@ -82,25 +82,6 @@ app = FastAPI()
82
  vqa_processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
83
  vqa_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
84
 
85
- # Load GPT model to rewrite answers (Phi-1.5)
86
- gpt_tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1_5")
87
- gpt_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1_5")
88
-
89
- def rewrite_answer(question, short_answer):
90
- prompt = f"Question: {question}\nShort Answer: {short_answer}\nFull Sentence:"
91
- inputs = gpt_tokenizer(prompt, return_tensors="pt")
92
- with torch.no_grad():
93
- outputs = gpt_model.generate(
94
- **inputs,
95
- max_new_tokens=50,
96
- do_sample=True,
97
- top_p=0.9,
98
- temperature=0.7,
99
- pad_token_id=gpt_tokenizer.eos_token_id
100
- )
101
- generated = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
102
- return generated.split("Full Sentence:")[-1].strip()
103
-
104
  def answer_question_from_image(image, question):
105
  if image is None or not question.strip():
106
  return "Please upload an image and ask a question.", None
@@ -111,18 +92,15 @@ def answer_question_from_image(image, question):
111
  predicted_id = outputs.logits.argmax(-1).item()
112
  short_answer = vqa_model.config.id2label[predicted_id]
113
 
114
- # Rewrite short answer to full sentence
115
- full_answer = rewrite_answer(question, short_answer)
116
-
117
  try:
118
- tts = gTTS(text=full_answer)
119
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
120
  tts.save(tmp.name)
121
  audio_path = tmp.name
122
  except Exception as e:
123
- return f"Answer: {full_answer}\n\n⚠️ Audio generation error: {e}", None
124
 
125
- return full_answer, audio_path
126
 
127
  def process_image_question(image: Image.Image, question: str):
128
  answer, audio_path = answer_question_from_image(image, question)
@@ -139,7 +117,7 @@ gui = gr.Interface(
139
  gr.Audio(label="Answer (Audio)", type="filepath")
140
  ],
141
  title="🧠 Image QA with Voice",
142
- description="Upload an image and ask a question. You'll get a full-sentence spoken answer."
143
  )
144
 
145
  app = gr.mount_gradio_app(app, gui, path="/")
 
70
  import os
71
  import shutil
72
  from PIL import Image
73
+ from transformers import ViltProcessor, ViltForQuestionAnswering
74
  from gtts import gTTS
75
  import torch
76
  import tempfile
 
82
  vqa_processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
83
  vqa_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  def answer_question_from_image(image, question):
86
  if image is None or not question.strip():
87
  return "Please upload an image and ask a question.", None
 
92
  predicted_id = outputs.logits.argmax(-1).item()
93
  short_answer = vqa_model.config.id2label[predicted_id]
94
 
 
 
 
95
  try:
96
+ tts = gTTS(text=short_answer)
97
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
98
  tts.save(tmp.name)
99
  audio_path = tmp.name
100
  except Exception as e:
101
+ return f"Answer: {short_answer}\n\n⚠️ Audio generation error: {e}", None
102
 
103
+ return short_answer, audio_path
104
 
105
  def process_image_question(image: Image.Image, question: str):
106
  answer, audio_path = answer_question_from_image(image, question)
 
117
  gr.Audio(label="Answer (Audio)", type="filepath")
118
  ],
119
  title="🧠 Image QA with Voice",
120
+ description="Upload an image and ask a question. You'll get an answer spoken out loud."
121
  )
122
 
123
  app = gr.mount_gradio_app(app, gui, path="/")