xxx1 commited on
Commit
949552b
1 Parent(s): cb6669b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import gradio as gr
3
+ import torch
4
+ import requests
5
+ from models.zhclip import ZhCLIPProcessor, ZhCLIPModel # From https://www.github.com/yue-gang/ZH-CLIP
6
+
7
+ version = 'nlpcver/zh-clip-vit-roberta-large-patch14'
8
+ model = ZhCLIPModel.from_pretrained(version)
9
+ processor = ZhCLIPProcessor.from_pretrained(version)
10
+
11
+
12
+
13
+ def get_result(image,text):
14
+ inputs = processor(text=[text], images=image, return_tensors="pt", padding=True)
15
+
16
+ outputs = model(**inputs)
17
+ image_features = outputs.image_features
18
+ text_features = outputs.text_features
19
+ text_probs = (image_features @ text_features.T).softmax(dim=-1)
20
+ return text_probs
21
+ with gr.Blocks(
22
+ css="""
23
+ .message.svelte-w6rprc.svelte-w6rprc.svelte-w6rprc {font-size: 20px; margin-top: 20px}
24
+ #component-21 > div.wrap.svelte-w6rprc {height: 600px;}
25
+ """
26
+ ) as iface:
27
+ state = gr.State([])
28
+
29
+ with gr.Row():
30
+ with gr.Column(scale=1):
31
+ image_input = gr.Image(type="pil",label="VQA Image Input")
32
+ with gr.Row():
33
+ with gr.Column(scale=1):
34
+ chat_input = gr.Textbox(lines=1, label="VQA Question Input")
35
+ with gr.Row():
36
+ clear_button = gr.Button(value="Clear", interactive=True,width=30)
37
+ submit_button = gr.Button(
38
+ value="Submit", interactive=True, variant="primary"
39
+ )
40
+ '''
41
+ cap_submit_button = gr.Button(
42
+ value="Submit_CAP", interactive=True, variant="primary"
43
+ )
44
+ gpt3_submit_button = gr.Button(
45
+ value="Submit_GPT3", interactive=True, variant="primary"
46
+ )
47
+ '''
48
+ with gr.Column():
49
+ caption_output = gr.Textbox(lines=0, label="ITM")
50
+
51
+ chat_input.submit(
52
+ get_result,
53
+ [
54
+ image_input,
55
+ chat_input,
56
+ ],
57
+ [ caption_output],
58
+ )
59
+ clear_button.click(
60
+ lambda: ("", [],"","",""),
61
+ [],
62
+ [chat_input, state,caption_output],
63
+ queue=False,
64
+ )
65
+ submit_button.click(
66
+ get_result,
67
+ [
68
+ image_input,
69
+ chat_input,
70
+ ],
71
+ [caption_output],
72
+ )
73
+ iface.queue(concurrency_count=1, api_open=False, max_size=10)
74
+ iface.launch(enable_queue=True)