Spaces:
Running
Running
add web demo
Browse files- README.md +5 -6
- app.py +166 -0
- examples/1.jpg +0 -0
- examples/2.jpg +0 -0
- examples/3.jpg +0 -0
- examples/example_inputs.jsonl +3 -0
- style.css +7 -0
README.md
CHANGED
@@ -1,13 +1,12 @@
|
|
1 |
---
|
2 |
-
title: Visualglm
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license: mit
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Visualglm-6b
|
3 |
+
emoji: 💻
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.27.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/VisualGLM">VisualGLM</a>'''
|
11 |
+
|
12 |
+
MAINTENANCE_NOTICE='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/VisualGLM">https://github.com/THUDM/VisualGLM</a>. It would be recommended to check out the repo if you want to see the detail of our model and training process.'
|
15 |
+
|
16 |
+
import json
|
17 |
+
import requests
|
18 |
+
import base64
|
19 |
+
|
20 |
+
|
21 |
+
URL = os.environ.get("URL")
|
22 |
+
|
23 |
+
def process_image(image_prompt):
|
24 |
+
image = Image.open(image_prompt)
|
25 |
+
print(f"height:{image.height}, width:{image.width}")
|
26 |
+
resized_image = image.resize((224, 224), )
|
27 |
+
timestamp = int(time.time())
|
28 |
+
file_ext = os.path.splitext(image_prompt)[1]
|
29 |
+
filename = f"examples/{timestamp}{file_ext}"
|
30 |
+
resized_image.save(filename)
|
31 |
+
print(f"temporal filename {filename}")
|
32 |
+
with open(filename, "rb") as image_file:
|
33 |
+
encoded_img = str(base64.b64encode(image_file.read()), encoding='utf-8')
|
34 |
+
os.remove(filename)
|
35 |
+
return encoded_img
|
36 |
+
|
37 |
+
|
38 |
+
def is_chinese(text):
|
39 |
+
zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
|
40 |
+
return zh_pattern.search(text)
|
41 |
+
|
42 |
+
|
43 |
+
def post(
|
44 |
+
input_text,
|
45 |
+
temperature,
|
46 |
+
top_p,
|
47 |
+
image_prompt,
|
48 |
+
result_previous
|
49 |
+
):
|
50 |
+
result_text = [(ele[0], ele[1]) for ele in result_previous]
|
51 |
+
for i in range(len(result_text)-1, -1, -1):
|
52 |
+
if result_text[i][0] == "":
|
53 |
+
del result_text[i]
|
54 |
+
print(f"history {result_text}")
|
55 |
+
|
56 |
+
is_zh = is_chinese(input_text)
|
57 |
+
|
58 |
+
if image_prompt is None:
|
59 |
+
print("Image empty")
|
60 |
+
if is_zh:
|
61 |
+
result_text.append((input_text, '图片为空!请上传图片并重试。'))
|
62 |
+
else:
|
63 |
+
result_text.append((input_text, 'Image empty! Please upload a image and retry.'))
|
64 |
+
return input_text, result_text
|
65 |
+
elif input_text == "":
|
66 |
+
print("Text empty")
|
67 |
+
result_text.append((input_text, 'Text empty! Please enter text and retry.'))
|
68 |
+
return "", result_text
|
69 |
+
|
70 |
+
headers = {
|
71 |
+
"Content-Type": "application/json; charset=UTF-8",
|
72 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
|
73 |
+
}
|
74 |
+
if image_prompt:
|
75 |
+
encoded_img = process_image(image_prompt)
|
76 |
+
else:
|
77 |
+
encoded_img = None
|
78 |
+
|
79 |
+
print('开始请求...')
|
80 |
+
data = json.dumps({
|
81 |
+
'text': input_text,
|
82 |
+
'image_prompt': encoded_img,
|
83 |
+
'temperature': temperature,
|
84 |
+
'top_p': top_p,
|
85 |
+
'history': result_text
|
86 |
+
})
|
87 |
+
try:
|
88 |
+
response = requests.request("POST", URL, headers=headers, data=data, timeout=(60, 100)).json()
|
89 |
+
except Exception as e:
|
90 |
+
print("error message", e)
|
91 |
+
if is_zh:
|
92 |
+
result_text.append((input_text, '超时!请稍等几分钟再重试。'))
|
93 |
+
else:
|
94 |
+
result_text.append((input_text, 'Timeout! Please wait a few minutes and retry.'))
|
95 |
+
return "", result_text
|
96 |
+
print('请求完毕...')
|
97 |
+
|
98 |
+
answer = str(response['result'])
|
99 |
+
result_text.append((input_text, answer))
|
100 |
+
print(result_text)
|
101 |
+
print('finished')
|
102 |
+
return "", result_text
|
103 |
+
|
104 |
+
|
105 |
+
def clear_fn(value):
|
106 |
+
return "", [("", "Hi, What do you want to know about this image?")], None
|
107 |
+
|
108 |
+
def clear_fn2(value):
|
109 |
+
return [("", "Hi, What do you want to know about this image?")]
|
110 |
+
|
111 |
+
|
112 |
+
def main():
|
113 |
+
gr.close_all()
|
114 |
+
examples = []
|
115 |
+
with open("./examples/example_inputs.jsonl") as f:
|
116 |
+
for line in f:
|
117 |
+
data = json.loads(line)
|
118 |
+
examples.append(data)
|
119 |
+
|
120 |
+
|
121 |
+
with gr.Blocks(css='style.css') as demo:
|
122 |
+
|
123 |
+
gr.Markdown(DESCRIPTION)
|
124 |
+
gr.Markdown(MAINTENANCE_NOTICE)
|
125 |
+
|
126 |
+
|
127 |
+
with gr.Row():
|
128 |
+
with gr.Column():
|
129 |
+
with gr.Group():
|
130 |
+
input_text = gr.Textbox(label='Input Text', placeholder='Please enter text prompt below and press ENTER.')
|
131 |
+
with gr.Row():
|
132 |
+
run_button = gr.Button('Generate')
|
133 |
+
clear_button = gr.Button('Clear')
|
134 |
+
|
135 |
+
image_prompt = gr.Image(type="filepath", label="Image Prompt", value=None)
|
136 |
+
with gr.Row():
|
137 |
+
temperature = gr.Slider(maximum=1, value=0.95, minimum=0, label='Temperature')
|
138 |
+
top_p = gr.Slider(maximum=1, value=0.7, minimum=0, label='Top P')
|
139 |
+
|
140 |
+
result_text = gr.components.Chatbot(label='Multi-round conversation History', value=[("", "Hi, What do you want to know about this image?")])
|
141 |
+
|
142 |
+
|
143 |
+
gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples],
|
144 |
+
inputs=[input_text, image_prompt],
|
145 |
+
label="Example Inputs (Click to insert an examplet into the input box)",
|
146 |
+
examples_per_page=3)
|
147 |
+
|
148 |
+
gr.Markdown(NOTES)
|
149 |
+
|
150 |
+
print(gr.__version__)
|
151 |
+
run_button.click(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text],
|
152 |
+
outputs=[input_text, result_text])
|
153 |
+
input_text.submit(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text],
|
154 |
+
outputs=[input_text, result_text])
|
155 |
+
clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, image_prompt])
|
156 |
+
image_prompt.change(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
157 |
+
|
158 |
+
print(gr.__version__)
|
159 |
+
|
160 |
+
|
161 |
+
demo.queue(concurrency_count=10)
|
162 |
+
demo.launch()
|
163 |
+
|
164 |
+
|
165 |
+
if __name__ == '__main__':
|
166 |
+
main()
|
examples/1.jpg
ADDED
examples/2.jpg
ADDED
examples/3.jpg
ADDED
examples/example_inputs.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{"id":1, "text": "描述这张图片", "image": "examples/1.jpg"}
|
2 |
+
{"id":2, "text": "描述这张图片中的场景", "image": "examples/2.jpg"}
|
3 |
+
{"id":3, "text": "Describe the image", "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 |
+
}
|