Tonic commited on
Commit
9a21df7
·
1 Parent(s): 6b167c1

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -119
app.py DELETED
@@ -1,119 +0,0 @@
1
- import gradio as gr
2
- import transformers
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- from transformers.generation import GenerationConfig
5
- import re
6
- import copy
7
- from pathlib import Path
8
- import secrets
9
- import torch
10
- import gc
11
- import os
12
- import io
13
- from io import BytesIO
14
- import base64
15
- import PIL
16
- from PIL import ImageDraw, UnidentifiedImageError
17
- from PIL import Image as PILImage
18
-
19
-
20
- image_dir = "saved_images"
21
- os.makedirs(image_dir, exist_ok=True)
22
- base_url = "https://huggingface.co/spaces/Tonic1/Official-Qwen-VL-Chat/blob/main/"
23
- model_name = "Qwen/Qwen-VL-Chat"
24
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
25
- model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", bf16=True, trust_remote_code=True).eval()
26
- model.generation_config = GenerationConfig.from_pretrained(model_name, trust_remote_code=True)
27
-
28
- BOX_TAG_PATTERN = r"<box>([\s\S]*?)</box>"
29
- PUNCTUATION = "!?。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』&#8203;``【oaicite:0】``&#8203;〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏."
30
-
31
- class ChatBot:
32
- def __init__(self, tokenizer, model):
33
- self.tokenizer = tokenizer
34
- self.model = model
35
- self.history = []
36
-
37
- def chat(self, image_path=None, text_query=None):
38
- query_elements = []
39
- if image_path:
40
- query_elements.append({'image': image_path})
41
- if text_query:
42
- query_elements.append({'text': text_query})
43
-
44
- query = self.tokenizer.from_list_format(query_elements)
45
- response, self.history = self.model.chat(self.tokenizer, query=query, history=self.history, max_new_tokens = 1200)
46
- return response
47
-
48
- def draw_boxes(self, response, image_path):
49
- boxes = re.findall(r'<box>\((\d+),(\d+)\),\((\d+),(\d+)\)</box>', response)
50
- if not boxes:
51
- return None
52
- try:
53
- with PILImage.open(image_path) as img:
54
- draw = ImageDraw.Draw(img)
55
- for box in boxes:
56
- x1, y1, x2, y2 = map(int, box)
57
- draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
58
- file_name = secrets.token_hex(10) + ".png"
59
- file_path = os.path.join(base_url, image_dir, file_name)
60
- img.save(file_path, format="PNG")
61
- return file_path
62
- except Exception as e:
63
- print(f"An error occurred while processing the image: {e}")
64
- return None
65
-
66
- def clean_response(self, response):
67
- return re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
68
-
69
- def clear_memory(self):
70
- if torch.cuda.is_available():
71
- torch.cuda.empty_cache()
72
- gc.collect()
73
-
74
- def chat_interface(text_query, file):
75
- chatbot = ChatBot(tokenizer, model)
76
- image_path = file.name if file is not None else None
77
- response = chatbot.chat(image_path=image_path, text_query=text_query)
78
-
79
- if "<box>" in response:
80
- image_file_path = chatbot.draw_boxes(response, image_path)
81
- text_response = chatbot.clean_response(response)
82
- chatbot.clear_memory()
83
- return [("Qwen-VL_Chat", text_response), ("Qwen-VL_Image", image_file_path)]
84
- else:
85
- chatbot.clear_memory()
86
- return [("Qwen-VL_Chat", response)]
87
-
88
- with gr.Blocks() as demo:
89
- gr.Markdown("""
90
- # 🙋🏻‍♂️欢迎来到🌟Tonic 的🦆Qwen-VL-Chat🤩Bot!🚀
91
- # 🙋🏻‍♂️Welcome to Tonic's🦆Qwen-VL-Chat🤩Bot!🚀
92
- 该WebUI基于Qwen-VL-Chat,实现聊天机器人功能。 但我必须解决它的很多问题,也许我也能获得一些荣誉。
93
- 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)
94
- 加入我们:🌟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) 做出贡献 )
95
- **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)
96
- 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)
97
- """)
98
- with gr.Row():
99
- with gr.Column(scale=1):
100
- chatbot_component = gr.Chatbot(label='🦆Qwen-VL-Chat')
101
- with gr.Column(scale=1):
102
- with gr.Row():
103
- query = gr.Textbox(lines=2, label='Input', placeholder="Type your message here...")
104
- file_upload = gr.File(label="Upload Image")
105
- submit_btn = gr.Button("Submit")
106
-
107
- submit_btn.click(
108
- fn=chat_interface,
109
- inputs=[query, file_upload],
110
- outputs=[chatbot_component]
111
- )
112
- gr.Markdown("""
113
- 注意:此演示受 Qwen-VL 原始许可证的约束。我们强烈建议用户不要故意生成或允许他人故意生成有害内容,
114
- 包括仇恨言论、暴力、色情、欺骗等。(注:本演示受Qwen-VL许可协议约束,强烈建议用户不要传播或允许他人传播以下内容,包括但不限于仇恨言论、暴力、色情、欺诈相关的有害信息 .)
115
- Note: This demo is governed by the original license of Qwen-VL. We strongly advise users not to knowingly generate or allow others to knowingly generate harmful content,
116
- including hate speech, violence, pornography, deception, etc. (Note: This demo is subject to the license agreement of Qwen-VL. We strongly advise users not to disseminate or allow others to disseminate the following content, including but not limited to hate speech, violence, pornography, and fraud-related harmful information.)
117
- """)
118
- if __name__ == "__main__":
119
- demo.launch()