Tonic commited on
Commit
8d1e886
·
1 Parent(s): 358b455

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -36,23 +36,20 @@ def clean_response(response: str) -> str:
36
  return response
37
 
38
  def chat_with_model(image_path=None, text_query=None, history=None):
39
- default_image_path = 'https://huggingface.co/spaces/Tonic1/Official-Qwen-VL-Chat/blob/main/2saj7l.png'
40
  if image_path:
41
- full_image_path = os.path.join(base_url, image_path) if base_url else image_path
42
- if os.path.isfile(full_image_path):
43
  try:
44
- with Image.open(full_image_path) as img:
45
- print(f"Image {full_image_path} opened successfully.")
46
  except UnidentifiedImageError:
47
- print(f"Error: The file at {full_image_path} is not a recognizable image.")
48
- full_image_path = default_image_path
49
  else:
50
- print(f"File not found: {full_image_path}")
51
- full_image_path = default_image_path
52
  else:
53
- full_image_path = default_image_path
54
- print("No image path provided, using default image.")
55
- image_path = default_image_path
56
  text_input = text_query if text_query else ""
57
  query_elements = [
58
  {'image': image_path},
@@ -78,6 +75,7 @@ def draw_boxes(image_path, response):
78
  draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
79
  return image
80
 
 
81
  def process_input(text=None, file=None, task_history=None):
82
  if task_history is None:
83
  task_history = []
@@ -85,6 +83,10 @@ def process_input(text=None, file=None, task_history=None):
85
  if file is not None:
86
  image_path = save_image(file, "uploaded_images")
87
  response = chat_with_model(image_path=image_path, text_query=text, history=task_history)
 
 
 
 
88
  task_history.append((text, response))
89
 
90
  if "<box>" in response:
@@ -96,7 +98,6 @@ def process_input(text=None, file=None, task_history=None):
96
  else:
97
  return [("bot", response), "text", None], task_history
98
  else:
99
- # Clean the response if it contains any box-like annotations
100
  clean_response = re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
101
  return [("bot", clean_response)], task_history
102
 
 
36
  return response
37
 
38
  def chat_with_model(image_path=None, text_query=None, history=None):
 
39
  if image_path:
40
+ if os.path.isfile(image_path):
 
41
  try:
42
+ with Image.open(image_path) as img:
43
+ print(f"Image {image_path} opened successfully.")
44
  except UnidentifiedImageError:
45
+ print(f"Error: The file at {image_path} is not a recognizable image.")
46
+ return "Error: Uploaded file is not a recognizable image."
47
  else:
48
+ print(f"File not found: {image_path}")
49
+ return "Error: Uploaded file not found."
50
  else:
51
+ print("No image path provided, using text-only mode.")
52
+
 
53
  text_input = text_query if text_query else ""
54
  query_elements = [
55
  {'image': image_path},
 
75
  draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
76
  return image
77
 
78
+ def process_input(text=None, file=None, task_history=None):
79
  def process_input(text=None, file=None, task_history=None):
80
  if task_history is None:
81
  task_history = []
 
83
  if file is not None:
84
  image_path = save_image(file, "uploaded_images")
85
  response = chat_with_model(image_path=image_path, text_query=text, history=task_history)
86
+ if "Error:" in response:
87
+ return [("bot", response)], task_history
88
+
89
+ response = chat_with_model(image_path=image_path, text_query=text, history=task_history)
90
  task_history.append((text, response))
91
 
92
  if "<box>" in response:
 
98
  else:
99
  return [("bot", response), "text", None], task_history
100
  else:
 
101
  clean_response = re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
102
  return [("bot", clean_response)], task_history
103