Tonic commited on
Commit
592760b
·
1 Parent(s): 9a9e036

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -34,15 +34,19 @@ def clean_response(response: str) -> str:
34
  return response
35
 
36
  def chat_with_model(image_path=None, text_query=None, history=None):
37
- try:
38
- with Image.open(image_path) as img:
39
- print(f"Image {image_path} opened successfully.")
40
- default_image_path = None
41
- default_text = ""
42
- image_input = image_path if image_path else default_image_path
43
- text_input = text_query if text_query else default_text
 
 
 
 
44
  query_elements = [
45
- {'image': image_input},
46
  {'text': text_input}
47
  ]
48
  try:
@@ -52,9 +56,10 @@ def chat_with_model(image_path=None, text_query=None, history=None):
52
  response = tokenizer.decode(output[0], skip_special_tokens=True)
53
  cleaned_response = clean_response(response)
54
  return cleaned_response
55
- except UnidentifiedImageError:
56
- return "Error: Unable to process the image. Please try a different image."
57
-
 
58
  def draw_boxes(image_path, response):
59
  image = Image.open(image_path)
60
  draw = ImageDraw.Draw(image)
 
34
  return response
35
 
36
  def chat_with_model(image_path=None, text_query=None, history=None):
37
+ default_image_path = 'path/to/default/image.jpg'
38
+ if image_path and os.path.isfile(image_path):
39
+ try:
40
+ with Image.open(image_path) as img:
41
+ print(f"Image {image_path} opened successfully.")
42
+ except UnidentifiedImageError:
43
+ print(f"Error: The file at {image_path} is not a recognizable image.")
44
+ image_path = default_image_path
45
+ else:
46
+ image_path = default_image_path
47
+ text_input = text_query if text_query else ""
48
  query_elements = [
49
+ {'image': image_path},
50
  {'text': text_input}
51
  ]
52
  try:
 
56
  response = tokenizer.decode(output[0], skip_special_tokens=True)
57
  cleaned_response = clean_response(response)
58
  return cleaned_response
59
+ except Exception as e:
60
+ print(f"An error occurred: {e}")
61
+ return "An error occurred while processing your request."
62
+
63
  def draw_boxes(image_path, response):
64
  image = Image.open(image_path)
65
  draw = ImageDraw.Draw(image)