Spaces:
Runtime error
Runtime error
Duplicate from lykeven/visualglm-6b
Browse filesCo-authored-by: Yan <[email protected]>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +209 -0
- examples/1.jpeg +0 -0
- examples/2.jpeg +0 -0
- examples/3.jpeg +0 -0
- examples/example_inputs.jsonl +3 -0
- style.css +7 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
duplicated_from: lykeven/visualglm-6b
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-6B">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 |
+
MAINTENANCE_NOTICE2 = '提示1: 如果应用报了“Something went wrong, connection error out”的错误,请关闭代理并重试。\n提示2: 如果你上传了很大的图片,比如10MB大小,那将需要一些时间来上传和处理,请耐心等待。'
|
14 |
+
|
15 |
+
NOTES = 'This app is adapted from <a href="https://github.com/THUDM/VisualGLM-6B">https://github.com/THUDM/VisualGLM-6B</a>. It would be recommended to check out the repo if you want to see the detail of our model.'
|
16 |
+
|
17 |
+
import json
|
18 |
+
import requests
|
19 |
+
import base64
|
20 |
+
import hashlib
|
21 |
+
|
22 |
+
default_chatbox = [("", "Hi, What do you want to know about this image?")]
|
23 |
+
|
24 |
+
URL = os.environ.get("URL")
|
25 |
+
|
26 |
+
def process_image(image_prompt):
|
27 |
+
image = Image.open(image_prompt)
|
28 |
+
print(f"height:{image.height}, width:{image.width}")
|
29 |
+
resized_image = image.resize((224, 224), )
|
30 |
+
timestamp = int(time.time())
|
31 |
+
file_ext = os.path.splitext(image_prompt)[1]
|
32 |
+
filename = f"examples/{timestamp}{file_ext}"
|
33 |
+
resized_image.save(filename)
|
34 |
+
print(f"temporal filename {filename}")
|
35 |
+
with open(filename, "rb") as image_file:
|
36 |
+
bytes = base64.b64encode(image_file.read())
|
37 |
+
encoded_img = str(bytes, encoding='utf-8')
|
38 |
+
image_hash = hashlib.sha256(bytes).hexdigest()
|
39 |
+
os.remove(filename)
|
40 |
+
return encoded_img, image_hash
|
41 |
+
|
42 |
+
|
43 |
+
def process_image_without_resize(image_prompt):
|
44 |
+
image = Image.open(image_prompt)
|
45 |
+
print(f"height:{image.height}, width:{image.width}")
|
46 |
+
timestamp = int(time.time())
|
47 |
+
file_ext = os.path.splitext(image_prompt)[1]
|
48 |
+
filename = f"examples/{timestamp}{file_ext}"
|
49 |
+
image.save(filename)
|
50 |
+
print(f"temporal filename {filename}")
|
51 |
+
with open(filename, "rb") as image_file:
|
52 |
+
bytes = base64.b64encode(image_file.read())
|
53 |
+
encoded_img = str(bytes, encoding='utf-8')
|
54 |
+
image_hash = hashlib.sha256(bytes).hexdigest()
|
55 |
+
os.remove(filename)
|
56 |
+
return encoded_img, image_hash
|
57 |
+
|
58 |
+
|
59 |
+
def is_chinese(text):
|
60 |
+
zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
|
61 |
+
return zh_pattern.search(text)
|
62 |
+
|
63 |
+
|
64 |
+
def post(
|
65 |
+
input_text,
|
66 |
+
temperature,
|
67 |
+
top_p,
|
68 |
+
image_prompt,
|
69 |
+
result_previous,
|
70 |
+
hidden_image
|
71 |
+
):
|
72 |
+
result_text = [(ele[0], ele[1]) for ele in result_previous]
|
73 |
+
for i in range(len(result_text)-1, -1, -1):
|
74 |
+
if result_text[i][0] == "":
|
75 |
+
del result_text[i]
|
76 |
+
print(f"history {result_text}")
|
77 |
+
|
78 |
+
is_zh = is_chinese(input_text)
|
79 |
+
|
80 |
+
if image_prompt is None:
|
81 |
+
print("Image empty")
|
82 |
+
if is_zh:
|
83 |
+
result_text.append((input_text, '图片为空!请上传图片并重试。'))
|
84 |
+
else:
|
85 |
+
result_text.append((input_text, 'Image empty! Please upload a image and retry.'))
|
86 |
+
return input_text, result_text, hidden_image
|
87 |
+
elif input_text == "":
|
88 |
+
print("Text empty")
|
89 |
+
result_text.append((input_text, 'Text empty! Please enter text and retry.'))
|
90 |
+
return "", result_text, hidden_image
|
91 |
+
|
92 |
+
headers = {
|
93 |
+
"Content-Type": "application/json; charset=UTF-8",
|
94 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
|
95 |
+
}
|
96 |
+
if image_prompt:
|
97 |
+
encoded_img, image_hash = process_image_without_resize(image_prompt)
|
98 |
+
print(f"image_hash:{image_hash}, hidden_image_hash:{hidden_image}")
|
99 |
+
|
100 |
+
if hidden_image is not None and image_hash != hidden_image:
|
101 |
+
print("image has been update")
|
102 |
+
result_text = []
|
103 |
+
hidden_image = image_hash
|
104 |
+
else:
|
105 |
+
encoded_img = None
|
106 |
+
|
107 |
+
print('开始请求...')
|
108 |
+
data = json.dumps({
|
109 |
+
'text': input_text,
|
110 |
+
'image_prompt': encoded_img,
|
111 |
+
'temperature': temperature,
|
112 |
+
'top_p': top_p,
|
113 |
+
'history': result_text
|
114 |
+
})
|
115 |
+
try:
|
116 |
+
response = requests.request("POST", URL, headers=headers, data=data, timeout=(60, 100)).json()
|
117 |
+
except Exception as e:
|
118 |
+
print("error message", e)
|
119 |
+
if is_zh:
|
120 |
+
result_text.append((input_text, '超时!请稍等几分钟再重试。'))
|
121 |
+
else:
|
122 |
+
result_text.append((input_text, 'Timeout! Please wait a few minutes and retry.'))
|
123 |
+
return "", result_text, hidden_image
|
124 |
+
print('请求完毕...')
|
125 |
+
# response = {'result':input_text}
|
126 |
+
|
127 |
+
answer = str(response['result'])
|
128 |
+
result_text.append((input_text, answer))
|
129 |
+
print(result_text)
|
130 |
+
print('finished')
|
131 |
+
return "", result_text, hidden_image
|
132 |
+
|
133 |
+
|
134 |
+
def clear_fn(value):
|
135 |
+
return "", default_chatbox, None
|
136 |
+
|
137 |
+
def clear_fn2(value):
|
138 |
+
return default_chatbox
|
139 |
+
|
140 |
+
def io_fn(a, b, c):
|
141 |
+
print(f"call io_fn")
|
142 |
+
return a, b
|
143 |
+
|
144 |
+
|
145 |
+
def change_language(value):
|
146 |
+
if value == "Change hint to English":
|
147 |
+
return "提示变为中文", MAINTENANCE_NOTICE1
|
148 |
+
else:
|
149 |
+
return "Change hint to English", MAINTENANCE_NOTICE2
|
150 |
+
|
151 |
+
|
152 |
+
def main():
|
153 |
+
gr.close_all()
|
154 |
+
examples = []
|
155 |
+
with open("./examples/example_inputs.jsonl") as f:
|
156 |
+
for line in f:
|
157 |
+
data = json.loads(line)
|
158 |
+
examples.append(data)
|
159 |
+
|
160 |
+
|
161 |
+
with gr.Blocks(css='style.css') as demo:
|
162 |
+
|
163 |
+
with gr.Row():
|
164 |
+
with gr.Column(scale=4.5):
|
165 |
+
with gr.Group():
|
166 |
+
input_text = gr.Textbox(label='Input Text', placeholder='Please enter text prompt below and press ENTER.')
|
167 |
+
with gr.Row():
|
168 |
+
run_button = gr.Button('Generate')
|
169 |
+
clear_button = gr.Button('Clear')
|
170 |
+
|
171 |
+
image_prompt = gr.Image(type="filepath", label="Image Prompt", value=None)
|
172 |
+
with gr.Row():
|
173 |
+
temperature = gr.Slider(maximum=1, value=0.8, minimum=0, label='Temperature')
|
174 |
+
top_p = gr.Slider(maximum=1, value=0.4, minimum=0, label='Top P')
|
175 |
+
with gr.Group():
|
176 |
+
with gr.Row():
|
177 |
+
with gr.Column(scale=7):
|
178 |
+
maintenance_notice = gr.Markdown(MAINTENANCE_NOTICE1)
|
179 |
+
with gr.Column(scale=2):
|
180 |
+
change_button = gr.Button('Change hint to English', visible=False)
|
181 |
+
with gr.Column(scale=5.5):
|
182 |
+
result_text = gr.components.Chatbot(label='Multi-round conversation History', value=[("", "Hi, What do you want to know about this image?")]).style(height=550)
|
183 |
+
hidden_image_hash = gr.Textbox(visible=False)
|
184 |
+
|
185 |
+
gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples],
|
186 |
+
inputs=[input_text, image_prompt],
|
187 |
+
label="Example Inputs (Click to insert an examplet into the input box)",
|
188 |
+
examples_per_page=3)
|
189 |
+
|
190 |
+
gr.Markdown(NOTES)
|
191 |
+
|
192 |
+
print(gr.__version__)
|
193 |
+
run_button.click(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text, hidden_image_hash],
|
194 |
+
outputs=[input_text, result_text, hidden_image_hash])
|
195 |
+
input_text.submit(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text, hidden_image_hash],
|
196 |
+
outputs=[input_text, result_text, hidden_image_hash])
|
197 |
+
clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, image_prompt])
|
198 |
+
image_prompt.upload(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
199 |
+
image_prompt.clear(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
200 |
+
|
201 |
+
print(gr.__version__)
|
202 |
+
|
203 |
+
|
204 |
+
demo.queue(concurrency_count=10)
|
205 |
+
demo.launch()
|
206 |
+
|
207 |
+
|
208 |
+
if __name__ == '__main__':
|
209 |
+
main()
|
examples/1.jpeg
ADDED
![]() |
examples/2.jpeg
ADDED
![]() |
examples/3.jpeg
ADDED
![]() |
examples/example_inputs.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{"id":1, "text": "描述一下这个场景", "image": "examples/1.jpeg"}
|
2 |
+
{"id":2, "text": "What object it is?", "image": "examples/2.jpeg"}
|
3 |
+
{"id":3, "text": "这张图片描述了什么", "image": "examples/3.jpeg"}
|
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 |
+
}
|