Spaces:
Sleeping
Sleeping
Create appImage.py
Browse files- appImage.py +34 -0
appImage.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import torch
|
| 7 |
+
import fitz # PyMuPDF for PDF
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# ========== Image QA Setup ==========
|
| 11 |
+
vqa_processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 12 |
+
vqa_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 13 |
+
def answer_question_from_image(image, question):
|
| 14 |
+
if image is None or not question.strip():
|
| 15 |
+
return "Please upload an image and ask a question."
|
| 16 |
+
inputs = vqa_processor(image, question, return_tensors="pt")
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = vqa_model(**inputs)
|
| 19 |
+
predicted_id = outputs.logits.argmax(-1).item()
|
| 20 |
+
return vqa_model.config.id2label[predicted_id]
|
| 21 |
+
# ========== Gradio Interfaces ==========
|
| 22 |
+
|
| 23 |
+
img_interface = gr.Interface(
|
| 24 |
+
fn=answer_question_from_image,
|
| 25 |
+
inputs=[gr.Image(label="Upload Image"), gr.Textbox(label="Ask a Question")],
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="Image Question Answering"
|
| 28 |
+
)
|
| 29 |
+
# ========== Combine and Mount ==========
|
| 30 |
+
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="/")
|