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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -64,27 +64,24 @@ 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[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,17 +95,13 @@ def advanced_response(
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:
@@ -122,6 +115,8 @@ with gr.Blocks() as demo:
122
  label="Selecciona el modelo",
123
  )
124
  chatbot_1 = gr.Chatbot(label="Gemini", scale=2, height=300, type="messages")
 
 
125
  system_instruction_1 = gr.Textbox(
126
  placeholder="Escribe una instrucci贸n para el sistema...",
127
  label="Instrucci贸n del sistema",
@@ -129,10 +124,6 @@ with gr.Blocks() as demo:
129
  value="You are an assistant.",
130
  )
131
 
132
- with gr.Row():
133
- text_input_1 = gr.Textbox(placeholder="Escribe un mensaje...", show_label=False, scale=8)
134
- run_button_1 = gr.Button(value="Enviar", variant="primary", scale=1)
135
-
136
  run_button_1.click(
137
  fn=bot_response,
138
  inputs=[model_dropdown_1, system_instruction_1, text_input_1, chatbot_1],
@@ -146,20 +137,18 @@ with gr.Blocks() as demo:
146
  label="Select Model",
147
  )
148
  chatbot_2 = gr.Chatbot(label="Gemini", height=300, type="messages")
 
 
 
149
  system_instruction_2 = gr.Textbox(
150
  placeholder="Enter system instruction...",
151
  label="System Instruction",
152
  scale=8,
153
  )
154
 
155
- with gr.Row():
156
- text_input_2 = gr.Textbox(placeholder="Message or description...", show_label=False, scale=8)
157
- upload_button = gr.UploadButton(label="Upload Images", file_count="multiple", file_types=["image"])
158
- run_button_2 = gr.Button(value="Run", variant="primary", scale=1)
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(
 
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
  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:
 
115
  label="Selecciona el modelo",
116
  )
117
  chatbot_1 = gr.Chatbot(label="Gemini", scale=2, height=300, type="messages")
118
+ text_input_1 = gr.Textbox(placeholder="Escribe un mensaje...", show_label=False, scale=8)
119
+ run_button_1 = gr.Button(value="Enviar", variant="primary", scale=1)
120
  system_instruction_1 = gr.Textbox(
121
  placeholder="Escribe una instrucci贸n para el sistema...",
122
  label="Instrucci贸n del sistema",
 
124
  value="You are an assistant.",
125
  )
126
 
 
 
 
 
127
  run_button_1.click(
128
  fn=bot_response,
129
  inputs=[model_dropdown_1, system_instruction_1, text_input_1, chatbot_1],
 
137
  label="Select Model",
138
  )
139
  chatbot_2 = gr.Chatbot(label="Gemini", height=300, type="messages")
140
+ text_input_2 = gr.Textbox(placeholder="Message or description...", show_label=False, scale=8)
141
+ upload_button = gr.UploadButton(label="Upload Images", file_count="multiple", file_types=["image"])
142
+ run_button_2 = gr.Button(value="Run", variant="primary", scale=1)
143
  system_instruction_2 = gr.Textbox(
144
  placeholder="Enter system instruction...",
145
  label="System Instruction",
146
  scale=8,
147
  )
148
 
 
 
 
 
 
149
  run_button_2.click(
150
  fn=advanced_response,
151
+ inputs=[upload_button, model_dropdown_2, system_instruction_2, chatbot_2],
152
  outputs=[chatbot_2],
153
  )
154
  upload_button.upload(