Spaces:
Running
Running
fix example select
Browse files
app.py
CHANGED
@@ -17,7 +17,9 @@ NOTES = 'This app is adapted from <a href="https://github.com/THUDM/VisualGLM">h
|
|
17 |
import json
|
18 |
import requests
|
19 |
import base64
|
|
|
20 |
|
|
|
21 |
|
22 |
URL = os.environ.get("URL")
|
23 |
|
@@ -31,9 +33,11 @@ def process_image(image_prompt):
|
|
31 |
resized_image.save(filename)
|
32 |
print(f"temporal filename {filename}")
|
33 |
with open(filename, "rb") as image_file:
|
34 |
-
|
|
|
|
|
35 |
os.remove(filename)
|
36 |
-
return encoded_img
|
37 |
|
38 |
|
39 |
def is_chinese(text):
|
@@ -46,7 +50,8 @@ def post(
|
|
46 |
temperature,
|
47 |
top_p,
|
48 |
image_prompt,
|
49 |
-
result_previous
|
|
|
50 |
):
|
51 |
result_text = [(ele[0], ele[1]) for ele in result_previous]
|
52 |
for i in range(len(result_text)-1, -1, -1):
|
@@ -62,18 +67,24 @@ def post(
|
|
62 |
result_text.append((input_text, '图片为空!请上传图片并重试。'))
|
63 |
else:
|
64 |
result_text.append((input_text, 'Image empty! Please upload a image and retry.'))
|
65 |
-
return input_text, result_text
|
66 |
elif input_text == "":
|
67 |
print("Text empty")
|
68 |
result_text.append((input_text, 'Text empty! Please enter text and retry.'))
|
69 |
-
return "", result_text
|
70 |
|
71 |
headers = {
|
72 |
"Content-Type": "application/json; charset=UTF-8",
|
73 |
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
|
74 |
}
|
75 |
if image_prompt:
|
76 |
-
encoded_img = process_image(image_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
else:
|
78 |
encoded_img = None
|
79 |
|
@@ -93,21 +104,26 @@ def post(
|
|
93 |
result_text.append((input_text, '超时!请稍等几分钟再重试。'))
|
94 |
else:
|
95 |
result_text.append((input_text, 'Timeout! Please wait a few minutes and retry.'))
|
96 |
-
return "", result_text
|
97 |
print('请求完毕...')
|
|
|
98 |
|
99 |
answer = str(response['result'])
|
100 |
result_text.append((input_text, answer))
|
101 |
print(result_text)
|
102 |
print('finished')
|
103 |
-
return "", result_text
|
104 |
|
105 |
|
106 |
def clear_fn(value):
|
107 |
-
return "",
|
108 |
|
109 |
def clear_fn2(value):
|
110 |
-
return
|
|
|
|
|
|
|
|
|
111 |
|
112 |
|
113 |
def change_language(value):
|
@@ -148,8 +164,7 @@ def main():
|
|
148 |
change_button = gr.Button('Change hint to English', visible=False)
|
149 |
with gr.Column(scale=5.5):
|
150 |
result_text = gr.components.Chatbot(label='Multi-round conversation History', value=[("", "Hi, What do you want to know about this image?")]).style(height=550)
|
151 |
-
|
152 |
-
|
153 |
|
154 |
gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples],
|
155 |
inputs=[input_text, image_prompt],
|
@@ -159,10 +174,10 @@ def main():
|
|
159 |
gr.Markdown(NOTES)
|
160 |
|
161 |
print(gr.__version__)
|
162 |
-
run_button.click(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text],
|
163 |
-
outputs=[input_text, result_text])
|
164 |
-
input_text.submit(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text],
|
165 |
-
outputs=[input_text, result_text])
|
166 |
clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, image_prompt])
|
167 |
image_prompt.upload(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
168 |
image_prompt.clear(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
|
|
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 |
|
|
|
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 is_chinese(text):
|
|
|
50 |
temperature,
|
51 |
top_p,
|
52 |
image_prompt,
|
53 |
+
result_previous,
|
54 |
+
hidden_image
|
55 |
):
|
56 |
result_text = [(ele[0], ele[1]) for ele in result_previous]
|
57 |
for i in range(len(result_text)-1, -1, -1):
|
|
|
67 |
result_text.append((input_text, '图片为空!请上传图片并重试。'))
|
68 |
else:
|
69 |
result_text.append((input_text, 'Image empty! Please upload a image and retry.'))
|
70 |
+
return input_text, result_text, hidden_image
|
71 |
elif input_text == "":
|
72 |
print("Text empty")
|
73 |
result_text.append((input_text, 'Text empty! Please enter text and retry.'))
|
74 |
+
return "", result_text, hidden_image
|
75 |
|
76 |
headers = {
|
77 |
"Content-Type": "application/json; charset=UTF-8",
|
78 |
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
|
79 |
}
|
80 |
if image_prompt:
|
81 |
+
encoded_img, image_hash = process_image(image_prompt)
|
82 |
+
print(f"image_hash:{image_hash}, hidden_image_hash:{hidden_image}")
|
83 |
+
|
84 |
+
if hidden_image is not None and image_hash != hidden_image:
|
85 |
+
print("image has been update")
|
86 |
+
result_text = []
|
87 |
+
hidden_image = image_hash
|
88 |
else:
|
89 |
encoded_img = None
|
90 |
|
|
|
104 |
result_text.append((input_text, '超时!请稍等几分钟再重试。'))
|
105 |
else:
|
106 |
result_text.append((input_text, 'Timeout! Please wait a few minutes and retry.'))
|
107 |
+
return "", result_text, hidden_image
|
108 |
print('请求完毕...')
|
109 |
+
# response = {'result':input_text}
|
110 |
|
111 |
answer = str(response['result'])
|
112 |
result_text.append((input_text, answer))
|
113 |
print(result_text)
|
114 |
print('finished')
|
115 |
+
return "", result_text, hidden_image
|
116 |
|
117 |
|
118 |
def clear_fn(value):
|
119 |
+
return "", default_chatbox, None
|
120 |
|
121 |
def clear_fn2(value):
|
122 |
+
return default_chatbox
|
123 |
+
|
124 |
+
def io_fn(a, b, c):
|
125 |
+
print(f"call io_fn")
|
126 |
+
return a, b
|
127 |
|
128 |
|
129 |
def change_language(value):
|
|
|
164 |
change_button = gr.Button('Change hint to English', visible=False)
|
165 |
with gr.Column(scale=5.5):
|
166 |
result_text = gr.components.Chatbot(label='Multi-round conversation History', value=[("", "Hi, What do you want to know about this image?")]).style(height=550)
|
167 |
+
hidden_image_hash = gr.Textbox(visible=False)
|
|
|
168 |
|
169 |
gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples],
|
170 |
inputs=[input_text, image_prompt],
|
|
|
174 |
gr.Markdown(NOTES)
|
175 |
|
176 |
print(gr.__version__)
|
177 |
+
run_button.click(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text, hidden_image_hash],
|
178 |
+
outputs=[input_text, result_text, hidden_image_hash])
|
179 |
+
input_text.submit(fn=post,inputs=[input_text, temperature, top_p, image_prompt, result_text, hidden_image_hash],
|
180 |
+
outputs=[input_text, result_text, hidden_image_hash])
|
181 |
clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, image_prompt])
|
182 |
image_prompt.upload(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|
183 |
image_prompt.clear(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
|