48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
import torch
|
|
from PIL import Image
|
|
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
|
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 = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
|
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16).to("cuda")
|
|
|
|
|
|
def image2text(image):
|
|
inp = Image.fromarray(image.astype('uint8'), 'RGB')
|
|
text = "a photography of"
|
|
inputs = processor(inp, text, 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():
|
|
box1 = gr.Image(label="图片")
|
|
with gr.Row():
|
|
button = gr.Button("提交", variant="primary")
|
|
box2 = gr.Textbox(label="文本")
|
|
|
|
button.click(fn=image2text, inputs=box1, outputs=box2)
|
|
examples = gr.Examples(examples=[['soccer.jpg']], inputs=[box1], label="例子")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
demo.queue(concurrency_count=3).launch(server_name = "0.0.0.0", server_port = 7019)
|