Spaces:
Running
Running
Update appImage.py
Browse files- appImage.py +19 -2
appImage.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from fastapi.responses import RedirectResponse
|
3 |
import gradio as gr
|
4 |
from transformers import pipeline, ViltProcessor, ViltForQuestionAnswering, AutoTokenizer, AutoModelForCausalLM
|
@@ -31,4 +31,21 @@ demo = gr.TabbedInterface( img_interface , "Image QA")
|
|
31 |
app = gr.mount_gradio_app(app, demo, path="/")
|
32 |
@app.get("/")
|
33 |
def root():
|
34 |
-
return RedirectResponse(url="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""from fastapi import FastAPI
|
2 |
from fastapi.responses import RedirectResponse
|
3 |
import gradio as gr
|
4 |
from transformers import pipeline, ViltProcessor, ViltForQuestionAnswering, AutoTokenizer, AutoModelForCausalLM
|
|
|
31 |
app = gr.mount_gradio_app(app, demo, path="/")
|
32 |
@app.get("/")
|
33 |
def root():
|
34 |
+
return RedirectResponse(url="/") """
|
35 |
+
from transformers import ViltProcessor, ViltForQuestionAnswering
|
36 |
+
import torch
|
37 |
+
|
38 |
+
# Load image QA model once
|
39 |
+
vqa_processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
40 |
+
vqa_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
41 |
+
|
42 |
+
def answer_question_from_image(image, question):
|
43 |
+
if image is None or not question.strip():
|
44 |
+
return "Please upload an image and ask a question."
|
45 |
+
|
46 |
+
inputs = vqa_processor(image, question, return_tensors="pt")
|
47 |
+
with torch.no_grad():
|
48 |
+
outputs = vqa_model(**inputs)
|
49 |
+
|
50 |
+
predicted_id = outputs.logits.argmax(-1).item()
|
51 |
+
return vqa_model.config.id2label[predicted_id]
|