Spaces:
Runtime error
Runtime error
add app
Browse files- app.py +195 -0
- examples/1.jpeg +0 -0
- examples/1.png +0 -0
- examples/2.jpeg +0 -0
- examples/2.jpg +0 -0
- examples/3.jpeg +0 -0
- examples/3.jpg +0 -0
- examples/example_inputs.jsonl +3 -0
- style.css +7 -0
app.py
ADDED
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
from PIL import Image
|
7 |
+
import base64
|
8 |
+
import time
|
9 |
+
|
10 |
+
DESCRIPTION = '''# <a href="https://github.com/THUDM/CogVLM">VisualGLM</a>'''
|
11 |
+
|
12 |
+
MAINTENANCE_NOTICE1 = 'Hint 1: If the app report "Something went wrong, connection error out", please turn off your proxy and retry.\nHint 2: If you upload a large size of image like 10MB, it may take some time to upload and process. Please be patient and wait.'
|
13 |
+
|
14 |
+
NOTES = 'This app is adapted from <a href="https://github.com/THUDM/CogVLM">https://github.com/THUDM/CogVLM</a>. It would be recommended to check out the repo if you want to see the detail of our model.'
|
15 |
+
|
16 |
+
import json
|
17 |
+
import requests
|
18 |
+
import base64
|
19 |
+
import hashlib
|
20 |
+
|
21 |
+
default_chatbox = [("", "Hi, What do you want to know about this image?")]
|
22 |
+
|
23 |
+
URL = os.environ.get("URL")
|
24 |
+
|
25 |
+
def process_image(image_prompt):
|
26 |
+
image = Image.open(image_prompt)
|
27 |
+
print(f"height:{image.height}, width:{image.width}")
|
28 |
+
resized_image = image.resize((224, 224), )
|
29 |
+
timestamp = int(time.time())
|
30 |
+
file_ext = os.path.splitext(image_prompt)[1]
|
31 |
+
filename = f"examples/{timestamp}{file_ext}"
|
32 |
+
resized_image.save(filename)
|
33 |
+
print(f"temporal filename {filename}")
|
34 |
+
with open(filename, "rb") as image_file:
|
35 |
+
bytes = base64.b64encode(image_file.read())
|
36 |
+
encoded_img = str(bytes, encoding='utf-8')
|
37 |
+
image_hash = hashlib.sha256(bytes).hexdigest()
|
38 |
+
os.remove(filename)
|
39 |
+
return encoded_img, image_hash
|
40 |
+
|
41 |
+
|
42 |
+
def process_image_without_resize(image_prompt):
|
43 |
+
image = Image.open(image_prompt)
|
44 |
+
print(f"height:{image.height}, width:{image.width}")
|
45 |
+
timestamp = int(time.time())
|
46 |
+
file_ext = os.path.splitext(image_prompt)[1]
|
47 |
+
filename = f"examples/{timestamp}{file_ext}"
|
48 |
+
image.save(filename)
|
49 |
+
print(f"temporal filename {filename}")
|
50 |
+
with open(filename, "rb") as image_file:
|
51 |
+
bytes = base64.b64encode(image_file.read())
|
52 |
+
encoded_img = str(bytes, encoding='utf-8')
|
53 |
+
image_hash = hashlib.sha256(bytes).hexdigest()
|
54 |
+
os.remove(filename)
|
55 |
+
return encoded_img, image_hash
|
56 |
+
|
57 |
+
|
58 |
+
def is_chinese(text):
|
59 |
+
zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
|
60 |
+
return zh_pattern.search(text)
|
61 |
+
|
62 |
+
|
63 |
+
def post(
|
64 |
+
input_text,
|
65 |
+
temperature,
|
66 |
+
top_p,
|
67 |
+
image_prompt,
|
68 |
+
result_previous,
|
69 |
+
hidden_image
|
70 |
+
):
|
71 |
+
result_text = [(ele[0], ele[1]) for ele in result_previous]
|
72 |
+
for i in range(len(result_text)-1, -1, -1):
|
73 |
+
if result_text[i][0] == "":
|
74 |
+
del result_text[i]
|
75 |
+
print(f"history {result_text}")
|
76 |
+
|
77 |
+
is_zh = is_chinese(input_text)
|
78 |
+
|
79 |
+
if image_prompt is None:
|
80 |
+
print("Image empty")
|
81 |
+
if is_zh:
|
82 |
+
result_text.append((input_text, '图片为空!请上传图片并重试。'))
|
83 |
+
else:
|
84 |
+
result_text.append((input_text, 'Image empty! Please upload a image and retry.'))
|
85 |
+
return input_text, result_text, hidden_image
|
86 |
+
elif input_text == "":
|
87 |
+
print("Text empty")
|
88 |
+
result_text.append((input_text, 'Text empty! Please enter text and retry.'))
|
89 |
+
return "", result_text, hidden_image
|
90 |
+
|
91 |
+
headers = {
|
92 |
+
"Content-Type": "application/json; charset=UTF-8",
|
93 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
|
94 |
+
}
|
95 |
+
if image_prompt:
|
96 |
+
encoded_img, image_hash = process_image_without_resize(image_prompt)
|
97 |
+
print(f"image_hash:{image_hash}, hidden_image_hash:{hidden_image}")
|
98 |
+
|
99 |
+
if hidden_image is not None and image_hash != hidden_image:
|
100 |
+
print("image has been update")
|
101 |
+
result_text = []
|
102 |
+
hidden_image = image_hash
|
103 |
+
else:
|
104 |
+
encoded_img = None
|
105 |
+
|
106 |
+
print('开始请求...')
|
107 |
+
data = json.dumps({
|
108 |
+
'text': input_text,
|
109 |
+
'image': encoded_img,
|
110 |
+
'temperature': temperature,
|
111 |
+
'top_p': top_p,
|
112 |
+
'history': result_text
|
113 |
+
})
|
114 |
+
try:
|
115 |
+
response = requests.request("POST", URL, headers=headers, data=data, timeout=(60, 100)).json()
|
116 |
+
except Exception as e:
|
117 |
+
print("error message", e)
|
118 |
+
if is_zh:
|
119 |
+
result_text.append((input_text, '超时!请稍等几分钟再重试。'))
|
120 |
+
else:
|
121 |
+
result_text.append((input_text, 'Timeout! Please wait a few minutes and retry.'))
|
122 |
+
return "", result_text, hidden_image
|
123 |
+
print('请求完毕...')
|
124 |
+
# response = {'result':input_text}
|
125 |
+
|
126 |
+
answer = str(response['result'])
|
127 |
+
result_text.append((input_text, answer))
|
128 |
+
print(result_text)
|
129 |
+
print('finished')
|
130 |
+
return "", result_text, hidden_image
|
131 |
+
|
132 |
+
|
133 |
+
def clear_fn(value):
|
134 |
+
return "", default_chatbox, None
|
135 |
+
|
136 |
+
def clear_fn2(value):
|
137 |
+
return default_chatbox
|
138 |
+
|
139 |
+
|
140 |
+
def main():
|
141 |
+
gr.close_all()
|
142 |
+
examples = []
|
143 |
+
with open("./examples/example_inputs.jsonl") as f:
|
144 |
+
for line in f:
|
145 |
+
data = json.loads(line)
|
146 |
+
examples.append(data)
|
147 |
+
|
148 |
+
|
149 |
+
with gr.Blocks(css='style.css') as demo:
|
150 |
+
|
151 |
+
with gr.Row():
|
152 |
+
with gr.Column(scale=4.5):
|
153 |
+
with gr.Group():
|
154 |
+
input_text = gr.Textbox(label='Input Text', placeholder='Please enter text prompt below and press ENTER.')
|
155 |
+
with gr.Row():
|
156 |
+
run_button = gr.Button('Generate')
|
157 |
+
clear_button = gr.Button('Clear')
|
158 |
+
|
159 |
+
image_prompt = gr.Image(type="filepath", label="Image Prompt", value=None)
|
160 |
+
with gr.Row():
|
161 |
+
temperature = gr.Slider(maximum=1, value=0.8, minimum=0, label='Temperature')
|
162 |
+
top_p = gr.Slider(maximum=1, value=0.4, minimum=0, label='Top P')
|
163 |
+
with gr.Group():
|
164 |
+
with gr.Row():
|
165 |
+
with gr.Column(scale=7):
|
166 |
+
maintenance_notice = gr.Markdown(MAINTENANCE_NOTICE1)
|
167 |
+
with gr.Column(scale=2):
|
168 |
+
change_button = gr.Button('Change hint to English', visible=False)
|
169 |
+
with gr.Column(scale=5.5):
|
170 |
+
result_text = gr.components.Chatbot(label='Multi-round conversation History', value=[("", "Hi, What do you want to know about this image?")]).style(height=550)
|
171 |
+
hidden_image_hash = gr.Textbox(visible=False)
|
172 |
+
|
173 |
+
gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples],
|
174 |
+
inputs=[input_text, image_prompt],
|
175 |
+
label="Example Inputs (Click to insert an examplet into the input box)",
|
176 |
+
examples_per_page=3)
|
177 |
+
|
178 |
+
gr.Markdown(NOTES)
|
179 |
+
|
180 |
+
print(gr.__version__)
|
181 |
+
run_button.click(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text, hidden_image_hash],
|
182 |
+
outputs=[input_text, result_text, hidden_image_hash])
|
183 |
+
input_text.submit(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text, hidden_image_hash],
|
184 |
+
outputs=[input_text, result_text, hidden_image_hash])
|
185 |
+
clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, image_prompt])
|
186 |
+
image_prompt.upload(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
187 |
+
image_prompt.clear(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
188 |
+
|
189 |
+
print(gr.__version__)
|
190 |
+
|
191 |
+
demo.queue(concurrency_count=10)
|
192 |
+
demo.launch()
|
193 |
+
|
194 |
+
if __name__ == '__main__':
|
195 |
+
main()
|
examples/1.jpeg
ADDED
examples/1.png
ADDED
examples/2.jpeg
ADDED
examples/2.jpg
ADDED
examples/3.jpeg
ADDED
examples/3.jpg
ADDED
examples/example_inputs.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{"id":1, "text": "Describe this image", "image": "examples/1.png"}
|
2 |
+
{"id":2, "text": "What is written in the image?", "image": "examples/2.jpg"}
|
3 |
+
{"id":3, "text": "How many houses are there in this cartoon?", "image": "examples/3.jpg"}
|
style.css
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
img#visitor-badge {
|
5 |
+
display: block;
|
6 |
+
margin: auto;
|
7 |
+
}
|