Upload 5 files
Browse filesFiles changed (5)
- LICENSEAdded+21
- README.md+2-12
- api.pyAdded+44
- app.pyAdded+60
- requirements.txtAdded+8
123456789101112131415161718192021MIT License
Copyright (c) 2023 AI Anytime
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112---title: ImageTestemoji: 🐨colorFrom: indigocolorTo: purplesdk: streamlitsdk_version: 1.38.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference12# Visual-Question-Answering-API-and-AppVisual Question Answering API and App using ViLT, Fast API, and Streamlit.
1234567891011121314151617181920212223242526272829303132333435363738394041424344from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse, RedirectResponse
from transformers import ViltProcessor, ViltForQuestionAnswering
from PIL import Image
import requests
import io
app = FastAPI(title="Visual Question and Answering API", version="0.0.1")
#Loading the model and tokenizer
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
def get_answer(image, text):
try:
# Load and process the image
img = Image.open(io.BytesIO(image)).convert("RGB")
# Prepare inputs
encoding = processor(img, text, return_tensors="pt")
# Forward pass
outputs = model(**encoding)
logits = outputs.logits
idx = logits.argmax(-1).item()
answer = model.config.id2label[idx]
return answer
except Exception as e:
return str(e)
@app.get("/", include_in_schema=False)
async def index():
return RedirectResponse(url="/docs")
@app.post("/answer")
async def process_image(image: UploadFile = File(...), text: str = None):
try:
answer = get_answer(await image.read(), text)
return JSONResponse({"Answer": answer})
except Exception as e:
return JSONResponse({"Sorry, please reach out to the Admin!": str(e)})123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import streamlit as st
from PIL import Image
import requests
from io import BytesIO
from transformers import ViltProcessor, ViltForQuestionAnswering
# Set page layout to wide
st.set_page_config(layout="wide")
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
def get_answer(image, text):
try:
# Load and process the image
img = Image.open(BytesIO(image)).convert("RGB")
# Prepare inputs
encoding = processor(img, text, return_tensors="pt")
# Forward pass
outputs = model(**encoding)
logits = outputs.logits
idx = logits.argmax(-1).item()
answer = model.config.id2label[idx]
return answer
except Exception as e:
return str(e)
# Set up the Streamlit app
st.title("Visual Question Answering")
st.write("Upload an image and enter a question to get an answer.")
# Create columns for image upload and input fields
col1, col2 = st.columns(2)
# Image upload
with col1:
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
st.image(uploaded_file, use_column_width=True)
# Question input
with col2:
question = st.text_input("Question")
# Process the image and question when both are provided
if uploaded_file and question is not None:
if st.button("Ask Question"):
image = Image.open(uploaded_file)
image_byte_array = BytesIO()
image.save(image_byte_array, format='JPEG')
image_bytes = image_byte_array.getvalue()
# Get the answer
answer = get_answer(image_bytes, question)
# Display the answer
st.success("Answer: " + answer)12345678transformers
torch
requests
Pillow
fastapi
uvicorn
streamlit
python-multipartLICENSEAdded+21
README.md+2-12
api.pyAdded+44
app.pyAdded+60
requirements.txtAdded+8