zxcgqq commited on
Commit
f4a6fe2
·
1 Parent(s): 1e6d5fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -18
app.py CHANGED
@@ -129,21 +129,8 @@ class ConversationBot:
129
  return state, state
130
 
131
  def run_image(self, image, state, txt, lang):
132
- image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png")
133
- print("======>Auto Resize Image...")
134
- img = Image.open(image.name)
135
- width, height = img.size
136
- ratio = min(512 / width, 512 / height)
137
- width_new, height_new = (round(width * ratio), round(height * ratio))
138
- width_new = int(np.round(width_new / 64.0)) * 64
139
- height_new = int(np.round(height_new / 64.0)) * 64
140
- img = img.resize((width_new, height_new))
141
- img = img.convert('RGB')
142
- img.save(image_filename, "PNG")
143
- print(f"Resize image form {width}x{height} to {width_new}x{height_new}")
144
- description = self.models['ImageCaptioning'].inference(image_filename)
145
  if lang == 'Chinese':
146
- Human_prompt = f'\nHuman: 提供一张名为 {image_filename}的图片。它的描述是: {description}。 这些信息帮助你理解这个图像,但是你应该使用工具来完成下面的任务,而不是直接从我的描述中想象。 如果你明白了, 说 \"收到\". \n'
147
  AI_prompt = "收到。 "
148
  else:
149
  Human_prompt = f'\nHuman: provide a figure named {image_filename}. The description is: {description}. This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n'
@@ -176,9 +163,7 @@ class ConversationBot:
176
 
177
  return gr.update(visible = True)
178
 
179
- bot = ConversationBot({'Text2Image': 'cpu',
180
- 'ImageCaptioning': 'cpu'
181
- })
182
 
183
  with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
184
  gr.Markdown("<h3><center>Visual ChatGPT</center></h3>")
@@ -234,7 +219,6 @@ with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
234
  txt.submit(lambda: "", None, txt)
235
  run.click(bot.run_text, [txt, state], [chatbot, state])
236
  run.click(lambda: "", None, txt)
237
- btn.upload(bot.run_image, [btn, state, txt, lang], [chatbot, state, txt])
238
  clear.click(bot.memory.clear)
239
  clear.click(lambda: [], None, chatbot)
240
  clear.click(lambda: [], None, state)
 
129
  return state, state
130
 
131
  def run_image(self, image, state, txt, lang):
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  if lang == 'Chinese':
133
+ Human_prompt = f'\nHuman: 提供一张名为 123的图片。它的描述是: 一只猫和一只狗。 这些信息帮助你理解这个图像,但是你应该使用工具来完成下面的任务,而不是直接从我的描述中想象。 如果你明白了, 说 \"收到\". \n'
134
  AI_prompt = "收到。 "
135
  else:
136
  Human_prompt = f'\nHuman: provide a figure named {image_filename}. The description is: {description}. This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n'
 
163
 
164
  return gr.update(visible = True)
165
 
166
+ bot = ConversationBot({})
 
 
167
 
168
  with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
169
  gr.Markdown("<h3><center>Visual ChatGPT</center></h3>")
 
219
  txt.submit(lambda: "", None, txt)
220
  run.click(bot.run_text, [txt, state], [chatbot, state])
221
  run.click(lambda: "", None, txt)
 
222
  clear.click(bot.memory.clear)
223
  clear.click(lambda: [], None, chatbot)
224
  clear.click(lambda: [], None, state)