Tonic commited on
Commit
fddbec9
1 Parent(s): 751b072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -1
app.py CHANGED
@@ -103,6 +103,29 @@ def process_speech(input_language, audio_input):
103
  except Exception as e :
104
  return f"{e}"
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  def process_image(image_input):
107
  # Initialize the Gradio client with the URL of the Gradio server
108
  client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/pqjvl/")
@@ -304,7 +327,9 @@ def process_and_query(input_language=None, audio_input=None, image_input=None, t
304
 
305
  # Process image input
306
  if image_input is not None:
307
- image_text = process_image(image_input)
 
 
308
  combined_text += "\n\n**Image Input:**\n" + image_text
309
 
310
  # Process audio input
 
103
  except Exception as e :
104
  return f"{e}"
105
 
106
+ def save_image(image_input, output_dir="saved_images"):
107
+ if not os.path.exists(output_dir):
108
+ os.makedirs(output_dir)
109
+
110
+ # Generate a unique file name
111
+ file_name = f"image_{int(time.time())}.png"
112
+ file_path = os.path.join(output_dir, file_name)
113
+
114
+ # Check the type of image_input and handle accordingly
115
+ if isinstance(image_input, np.ndarray): # If image_input is a NumPy array
116
+ Image.fromarray(image_input).save(file_path)
117
+ elif isinstance(image_input, Image.Image): # If image_input is a PIL image
118
+ image_input.save(file_path)
119
+ elif isinstance(image_input, str) and image_input.startswith('data:image'): # If image_input is a base64 string
120
+ image_data = base64.b64decode(image_input.split(',')[1])
121
+ with open(file_path, 'wb') as f:
122
+ f.write(image_data)
123
+ else:
124
+ raise ValueError("Unsupported image format")
125
+
126
+ return file_path
127
+
128
+
129
  def process_image(image_input):
130
  # Initialize the Gradio client with the URL of the Gradio server
131
  client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/pqjvl/")
 
327
 
328
  # Process image input
329
  if image_input is not None:
330
+ # Convert image_input to a file path
331
+ image_file_path = save_image(image_input)
332
+ image_text = process_image(image_file_path)
333
  combined_text += "\n\n**Image Input:**\n" + image_text
334
 
335
  # Process audio input