JeCabrera commited on
Commit
893883c
verified
1 Parent(s): fd4c4a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -64,24 +64,27 @@ def cache_pil_image(image: Image.Image) -> str:
64
  image.save(image_path, "JPEG")
65
  return image_path
66
 
67
- def upload(files: Optional[List[str]], chatbot: List[dict]) -> List[dict]:
68
- for file in files:
69
- image = Image.open(file).convert("RGB")
70
- image_preview = preprocess_image(image)
71
- if image_preview:
72
- gr.Image(image_preview).render()
73
- image_path = cache_pil_image(image)
74
- chatbot.append({"role": "user", "content": f"Uploaded image: {image_path}"})
75
  return chatbot
76
 
77
  def advanced_response(
78
- files: Optional[List[str]],
 
79
  model_choice: str,
80
  system_instruction: str,
81
  chatbot: List[dict],
82
  ):
83
- if not files:
84
- return chatbot
 
 
85
 
86
  model = genai.GenerativeModel(
87
  model_name=model_choice,
@@ -95,13 +98,17 @@ def advanced_response(
95
  chat = model.start_chat(history=chatbot)
96
  chat.system_instruction = system_instruction
97
 
98
- images = [cache_pil_image(preprocess_image(Image.open(file))) for file in files]
99
- response = chat.generate_content(images, stream=True)
 
 
 
100
 
101
- chatbot.append({"role": "assistant", "content": ""})
102
- for chunk in response:
103
- chatbot[-1]["content"] += chunk.text
104
- yield chatbot
 
105
 
106
  # Construcci贸n de la interfaz con las dos pesta帽as originales
107
  with gr.Blocks() as demo:
@@ -152,7 +159,7 @@ with gr.Blocks() as demo:
152
 
153
  run_button_2.click(
154
  fn=advanced_response,
155
- inputs=[upload_button, model_dropdown_2, system_instruction_2, chatbot_2],
156
  outputs=[chatbot_2],
157
  )
158
  upload_button.upload(
 
64
  image.save(image_path, "JPEG")
65
  return image_path
66
 
67
+ def upload(files: Optional[List[gr.File]], chatbot: List[dict]) -> List[dict]:
68
+ if files:
69
+ for file in files:
70
+ image = Image.open(file.name).convert("RGB")
71
+ image_preview = preprocess_image(image)
72
+ if image_preview:
73
+ image_path = cache_pil_image(image)
74
+ chatbot.append({"role": "user", "content": f"Uploaded image: {image_path}"})
75
  return chatbot
76
 
77
  def advanced_response(
78
+ text_prompt: str,
79
+ files: Optional[List[gr.File]],
80
  model_choice: str,
81
  system_instruction: str,
82
  chatbot: List[dict],
83
  ):
84
+ if not text_prompt.strip() and not files:
85
+ chatbot.append({"role": "assistant", "content": "Please provide a prompt or upload an image."})
86
+ yield chatbot
87
+ return
88
 
89
  model = genai.GenerativeModel(
90
  model_name=model_choice,
 
98
  chat = model.start_chat(history=chatbot)
99
  chat.system_instruction = system_instruction
100
 
101
+ if text_prompt:
102
+ chatbot.append({"role": "user", "content": text_prompt})
103
+ if files:
104
+ images = [cache_pil_image(preprocess_image(Image.open(file.name))) for file in files]
105
+ chatbot.append({"role": "user", "content": f"Uploaded images: {', '.join(images)}"})
106
 
107
+ response = chat.send_message(text_prompt)
108
+ response.resolve()
109
+
110
+ chatbot.append({"role": "assistant", "content": response.text})
111
+ yield chatbot
112
 
113
  # Construcci贸n de la interfaz con las dos pesta帽as originales
114
  with gr.Blocks() as demo:
 
159
 
160
  run_button_2.click(
161
  fn=advanced_response,
162
+ inputs=[text_input_2, upload_button, model_dropdown_2, system_instruction_2, chatbot_2],
163
  outputs=[chatbot_2],
164
  )
165
  upload_button.upload(