46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
import torch
|
|
from PIL import Image
|
|
from transformers import BlipProcessor, BlipForQuestionAnswering
|
|
import gradio as gr
|
|
from gradio.themes.utils import sizes
|
|
|
|
|
|
theme = gr.themes.Default(radius_size=sizes.radius_none).set(
|
|
block_label_text_color = '#4D63FF',
|
|
block_title_text_color = '#4D63FF',
|
|
button_primary_text_color = '#4D63FF',
|
|
button_primary_background_fill='#FFFFFF',
|
|
button_primary_border_color='#4D63FF',
|
|
button_primary_background_fill_hover='#EDEFFF',
|
|
)
|
|
|
|
|
|
processor = BlipProcessor.from_pretrained("ybelkada/blip-vqa-capfilt-large")
|
|
model = BlipForQuestionAnswering.from_pretrained("ybelkada/blip-vqa-capfilt-large", torch_dtype=torch.float16).to("cuda")
|
|
|
|
def vqa(image, question):
|
|
inp = Image.fromarray(image.astype('uint8'), 'RGB')
|
|
inputs = processor(inp, question, return_tensors="pt").to("cuda", torch.float16)
|
|
out = model.generate(**inputs)
|
|
|
|
return processor.decode(out[0], skip_special_tokens=True)
|
|
|
|
with gr.Blocks(theme=theme, css="footer {visibility: hidden}") as demo:
|
|
gr.Markdown("""
|
|
<div align='center' ><font size='60'>图片问答</font></div>
|
|
""")
|
|
with gr.Row():
|
|
with gr.Column():
|
|
image = gr.Image(label="图片")
|
|
question = gr.Textbox(label="问题")
|
|
with gr.Row():
|
|
button = gr.Button("提交", variant="primary")
|
|
box2 = gr.Textbox(label="文本")
|
|
|
|
button.click(fn=vqa, inputs=[image, question], outputs=box2)
|
|
examples = gr.Examples(examples=[['demo.jpg', 'how many dogs are in the picture?']], inputs=[image, question], label="例子")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
demo.queue(concurrency_count=3).launch(server_name = "0.0.0.0", server_port = 7025)
|