Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,8 @@ from PIL import ImageDraw, UnidentifiedImageError
|
|
| 17 |
from PIL import Image as PILImage
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
| 20 |
base_url = "https://huggingface.co/spaces/Tonic1/Official-Qwen-VL-Chat"
|
| 21 |
model_name = "Qwen/Qwen-VL-Chat"
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
@@ -43,21 +45,27 @@ class ChatBot:
|
|
| 43 |
response, self.history = self.model.chat(self.tokenizer, query=query, history=self.history)
|
| 44 |
return response
|
| 45 |
|
| 46 |
-
def draw_boxes(self, response):
|
| 47 |
-
|
| 48 |
-
if
|
| 49 |
-
if not isinstance(image, PILImage.Image):
|
| 50 |
-
image = PILImage.fromarray(image.get_image())
|
| 51 |
-
buffered = io.BytesIO()
|
| 52 |
-
image.save(buffered, format="PNG")
|
| 53 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 54 |
-
return "data:image/png;base64," + img_str
|
| 55 |
-
else:
|
| 56 |
return None
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def clean_response(self, response):
|
| 59 |
return re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
|
| 60 |
-
|
| 61 |
def clear_memory(self):
|
| 62 |
if torch.cuda.is_available():
|
| 63 |
torch.cuda.empty_cache()
|
|
@@ -69,10 +77,10 @@ def chat_interface(text_query, file):
|
|
| 69 |
response = chatbot.chat(image_path=image_path, text_query=text_query)
|
| 70 |
|
| 71 |
if "<box>" in response:
|
| 72 |
-
|
| 73 |
text_response = chatbot.clean_response(response)
|
| 74 |
chatbot.clear_memory()
|
| 75 |
-
return [("Qwen-VL_Chat", text_response), ("Qwen-VL_Image",
|
| 76 |
else:
|
| 77 |
chatbot.clear_memory()
|
| 78 |
return [("Qwen-VL_Chat", response)]
|
|
@@ -108,4 +116,4 @@ Note: This demo is governed by the original license of Qwen-VL. We strongly advi
|
|
| 108 |
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.)
|
| 109 |
""")
|
| 110 |
if __name__ == "__main__":
|
| 111 |
-
demo.launch()
|
|
|
|
| 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"
|
| 23 |
model_name = "Qwen/Qwen-VL-Chat"
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
| 45 |
response, self.history = self.model.chat(self.tokenizer, query=query, history=self.history)
|
| 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(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()
|
|
|
|
| 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)]
|
|
|
|
| 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()
|