Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
# gr.load("models/ManishThota/InstructBlip-VQA").launch()
|
4 |
-
|
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")
|
20 |
|
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(
|
41 |
fn=gradio_predict,
|
@@ -48,4 +35,3 @@ iface = gr.Interface(
|
|
48 |
# Launch the app
|
49 |
iface.queue().launch(debug=True)
|
50 |
|
51 |
-
# demo.queue().launch(debug=True)
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
from PIL import Image
|
3 |
import torch
|
4 |
from transformers import BlipProcessor, BlipForQuestionAnswering
|
|
|
5 |
|
6 |
# Initialize the model and processor
|
7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
8 |
model = BlipForQuestionAnswering.from_pretrained("ManishThota/InstructBlip-VQA")
|
9 |
|
|
|
|
|
|
|
10 |
def predict_answer(image, question):
|
11 |
# Convert PIL image to RGB if not already
|
12 |
image = image.convert("RGB")
|
13 |
|
14 |
# Prepare inputs
|
15 |
+
encoding = processor(image, question, return_tensors="pt").to("cuda:0", torch.float16)
|
16 |
|
17 |
out = model.generate(**encoding)
|
18 |
generated_text = processor.decode(out[0], skip_special_tokens=True)
|
19 |
|
20 |
return generated_text
|
21 |
|
|
|
22 |
def gradio_predict(image, question):
|
23 |
answer = predict_answer(image, question)
|
24 |
return answer
|
25 |
+
|
|
|
|
|
|
|
|
|
|
|
26 |
# Define the Gradio interface
|
27 |
iface = gr.Interface(
|
28 |
fn=gradio_predict,
|
|
|
35 |
# Launch the app
|
36 |
iface.queue().launch(debug=True)
|
37 |
|
|