Create app.py
Browse filesstreamlit
torch
transformers
pillow
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# 设置模型和处理器
|
| 7 |
+
model_id = "OpenFace-CQUPT/Human_LLaVA"
|
| 8 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
# Streamlit 界面设置
|
| 12 |
+
st.title("Visual Question Answering App")
|
| 13 |
+
st.write("Upload an image and ask a question about it!")
|
| 14 |
+
|
| 15 |
+
# 图片上传
|
| 16 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 17 |
+
question = st.text_input("Ask a question about the image:")
|
| 18 |
+
|
| 19 |
+
# 处理输入并获取答案
|
| 20 |
+
if uploaded_image is not None and question:
|
| 21 |
+
image = Image.open(uploaded_image)
|
| 22 |
+
|
| 23 |
+
# 显示图片和问题
|
| 24 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 25 |
+
st.write("Question:", question)
|
| 26 |
+
|
| 27 |
+
# 使用模型生成答案
|
| 28 |
+
with st.spinner("Generating answer..."):
|
| 29 |
+
inputs = processor(images=image, text=question, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
output = model.generate(**inputs)
|
| 32 |
+
answer = processor.decode(output[0], skip_special_tokens=True)
|
| 33 |
+
|
| 34 |
+
# 显示答案
|
| 35 |
+
st.write("Answer:", answer)
|