ManishThota commited on
Commit
56be01d
·
verified ·
1 Parent(s): 143ba51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -5,11 +5,15 @@ import gradio as gr
5
  from PIL import Image
6
  import torch
7
  from transformers import BlipProcessor, BlipForQuestionAnswering
 
8
 
9
  # Initialize the model and processor
10
  processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
11
  model = BlipForQuestionAnswering.from_pretrained("ManishThota/InstructBlip-VQA")
12
 
 
 
 
13
  def predict_answer(image, question):
14
  # Convert PIL image to RGB if not already
15
  image = image.convert("RGB")
@@ -17,15 +21,20 @@ def predict_answer(image, question):
17
  # Prepare inputs
18
  encoding = processor(image, question, return_tensors="pt")
19
 
20
- out = model.generate(**encoding, max_length=512)
21
  generated_text = processor.decode(out[0], skip_special_tokens=True)
22
 
23
  return generated_text
24
 
25
 
 
 
 
 
26
  def gradio_predict(image, question):
27
- answer = predict_answer(image, question)
28
- return answer
 
29
 
30
  # Define the Gradio interface
31
  iface = gr.Interface(
 
5
  from PIL import Image
6
  import torch
7
  from transformers import BlipProcessor, BlipForQuestionAnswering
8
+ from concurrent.futures import ThreadPoolExecutor
9
 
10
  # Initialize the model and processor
11
  processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
12
  model = BlipForQuestionAnswering.from_pretrained("ManishThota/InstructBlip-VQA")
13
 
14
+ executor = ThreadPoolExecutor(max_workers=4)
15
+
16
+
17
  def predict_answer(image, question):
18
  # Convert PIL image to RGB if not already
19
  image = image.convert("RGB")
 
21
  # Prepare inputs
22
  encoding = processor(image, question, return_tensors="pt")
23
 
24
+ out = model.generate(**encoding)
25
  generated_text = processor.decode(out[0], skip_special_tokens=True)
26
 
27
  return generated_text
28
 
29
 
30
+ # def gradio_predict(image, question):
31
+ # answer = predict_answer(image, question)
32
+ # return answer
33
+
34
  def gradio_predict(image, question):
35
+ future = executor.submit(predict_answer, image, question)
36
+ return future.result()
37
+
38
 
39
  # Define the Gradio interface
40
  iface = gr.Interface(