Tonic commited on
Commit
afa548b
·
1 Parent(s): feee422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -84,13 +84,15 @@ def chat_with_model(image_path=None, text_query=None, history=None):
84
  return "An error occurred while processing your request."
85
 
86
  def draw_boxes(image_path, response):
87
- image = Image.open(image_path)
88
- draw = ImageDraw.Draw(image)
89
- boxes = re.findall(r'<box>\((\d+),(\d+)\),\((\d+),(\d+)\)</box>', response)
90
- for box in boxes:
91
- x1, y1, x2, y2 = map(int, box)
92
- draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
93
- return image
 
 
94
 
95
  def process_input(text=None, file=None, task_history=None):
96
  if task_history is None:
@@ -105,11 +107,8 @@ def process_input(text=None, file=None, task_history=None):
105
  task_history.append((text, response))
106
 
107
  if "<box>" in response and image_path:
108
- image_with_boxes = draw_boxes(image_path, response)
109
- buffered = BytesIO()
110
- image_with_boxes.save(buffered, format="PNG")
111
- img_str = buffered.getvalue()
112
- return [("bot", response, "data:image/png;base64," + base64.b64encode(img_str).decode())], task_history
113
  else:
114
  cleaned_response = clean_response(response)
115
  return [("bot", cleaned_response)], task_history
@@ -117,11 +116,11 @@ def process_input(text=None, file=None, task_history=None):
117
  with gr.Blocks() as demo:
118
  gr.Markdown("""
119
  # 🙋🏻‍♂️欢迎来到🌟Tonic 的🦆Qwen-VL-Chat🤩Bot!🚀
120
- # 🙋🏻‍♂️Welcome toTonic's Qwen-VL-Chat Bot!
121
  该WebUI基于Qwen-VL-Chat,实现聊天机器人功能。 但我必须解决它的很多问题,也许我也能获得一些荣誉。
122
  Qwen-VL-Chat 是一种多模式输入模型。 您可以使用此空间来测试当前模型 [qwen/Qwen-VL-Chat](https://huggingface.co/qwen/Qwen-VL-Chat) 您也可以使用 🧑🏻‍🚀qwen/Qwen-VL -通过克隆这个空间来聊天🚀。 🧬🔬🔍 只需点击这里:[重复空间](https://huggingface.co/spaces/Tonic1/VLChat?duplicate=true)
123
  加入我们:🌟TeamTonic🌟总是在制作很酷的演示! 在 👻Discord 上加入我们活跃的构建者🛠️社区:[Discord](https://discord.gg/nXx5wbX9) 在 🤗Huggingface 上:[TeamTonic](https://huggingface.co/TeamTonic) 和 [MultiTransformer](https:/ /huggingface.co/MultiTransformer) 在 🌐Github 上:[Polytonic](https://github.com/tonic-ai) 并为 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha) 做出贡献 )
124
- This WebUI is based on Qwen-VL-Chat, implementing chatbot functionalities. Qwen-VL-Chat is a multimodal input model. You can use this Space to test out the current model [qwen/Qwen-VL-Chat](https://huggingface.co/qwen/Qwen-VL-Chat) You can also use qwen/Qwen-VL-Chat🚀 by cloning this space. Simply click here: [Duplicate Space](https://huggingface.co/spaces/Tonic1/VLChat?duplicate=true)
125
  Join us: TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/nXx5wbX9) On Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On Github: [Polytonic](https://github.com/tonic-ai) & contribute to [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)
126
  """)
127
  with gr.Row():
 
84
  return "An error occurred while processing your request."
85
 
86
  def draw_boxes(image_path, response):
87
+ with Image.open(image_path) as image:
88
+ draw = ImageDraw.Draw(image)
89
+ boxes = re.findall(r'<box>\((\d+),(\d+)\),\((\d+),(\d+)\)</box>', response)
90
+ for box in boxes:
91
+ x1, y1, x2, y2 = map(int, box)
92
+ draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
93
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.png', dir=os.path.abspath("uploaded_images")) as temp_file:
94
+ image.save(temp_file, format="PNG")
95
+ return temp_file.name
96
 
97
  def process_input(text=None, file=None, task_history=None):
98
  if task_history is None:
 
107
  task_history.append((text, response))
108
 
109
  if "<box>" in response and image_path:
110
+ image_with_boxes_path = draw_boxes(image_path, response)
111
+ return [("bot", response), "image", image_with_boxes_path], task_history
 
 
 
112
  else:
113
  cleaned_response = clean_response(response)
114
  return [("bot", cleaned_response)], task_history
 
116
  with gr.Blocks() as demo:
117
  gr.Markdown("""
118
  # 🙋🏻‍♂️欢迎来到🌟Tonic 的🦆Qwen-VL-Chat🤩Bot!🚀
119
+ # 🙋🏻‍♂️Welcome to Tonic's🦆Qwen-VL-Chat🤩Bot!🚀
120
  该WebUI基于Qwen-VL-Chat,实现聊天机器人功能。 但我必须解决它的很多问题,也许我也能获得一些荣誉。
121
  Qwen-VL-Chat 是一种多模式输入模型。 您可以使用此空间来测试当前模型 [qwen/Qwen-VL-Chat](https://huggingface.co/qwen/Qwen-VL-Chat) 您也可以使用 🧑🏻‍🚀qwen/Qwen-VL -通过克隆这个空间来聊天🚀。 🧬🔬🔍 只需点击这里:[重复空间](https://huggingface.co/spaces/Tonic1/VLChat?duplicate=true)
122
  加入我们:🌟TeamTonic🌟总是在制作很酷的演示! 在 👻Discord 上加入我们活跃的构建者🛠️社区:[Discord](https://discord.gg/nXx5wbX9) 在 🤗Huggingface 上:[TeamTonic](https://huggingface.co/TeamTonic) 和 [MultiTransformer](https:/ /huggingface.co/MultiTransformer) 在 🌐Github 上:[Polytonic](https://github.com/tonic-ai) 并为 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha) 做出贡献 )
123
+ **You can upload a picture with query or specifically ask this model to draw boxes and frames (just keep trying until you get your returns)** . Qwen-VL-Chat is a multimodal input model. You can use this Space to test out the current model [qwen/Qwen-VL-Chat](https://huggingface.co/qwen/Qwen-VL-Chat) You can also use qwen/Qwen-VL-Chat🚀 by cloning this space. Simply click here: [Duplicate Space](https://huggingface.co/spaces/Tonic1/VLChat?duplicate=true)
124
  Join us: TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/nXx5wbX9) On Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On Github: [Polytonic](https://github.com/tonic-ai) & contribute to [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)
125
  """)
126
  with gr.Row():