JeCabrera commited on
Commit
ccfd058
verified
1 Parent(s): b6515bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -44,6 +44,7 @@ def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
44
  image = Image.open(file).convert('RGB')
45
  image_preview = preprocess_image(image)
46
  if image_preview:
 
47
  gr.Image(image_preview).render()
48
  image_path = cache_pil_image(image)
49
  chatbot.append(((image_path,), None))
@@ -57,31 +58,29 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
57
  def bot(
58
  files: Optional[List[str]],
59
  model_choice: str,
60
- use_system_instruction: str, # Controla si se usa la instrucci贸n del sistema
61
- system_instruction: str, # Texto de la instrucci贸n del sistema
62
  chatbot: CHAT_HISTORY
63
  ):
64
  if not GOOGLE_API_KEY:
65
  raise ValueError("GOOGLE_API_KEY is not set.")
66
 
 
67
  genai.configure(api_key=GOOGLE_API_KEY)
68
  generation_config = genai.types.GenerationConfig(
69
- temperature=0.7,
70
- max_output_tokens=8192,
71
- top_k=10,
72
- top_p=0.9
73
  )
74
 
75
  text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
76
  image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
77
-
78
- # Considerar la instrucci贸n del sistema solo si est谩 activada
79
- system_instruction_to_use = system_instruction if use_system_instruction == "Yes" else None
80
-
81
  model = genai.GenerativeModel(
82
  model_name=model_choice,
83
  generation_config=generation_config,
84
- system_instruction=system_instruction_to_use # Aplicar la instrucci贸n si corresponde
85
  )
86
 
87
  response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
@@ -94,10 +93,30 @@ def bot(
94
  time.sleep(0.01)
95
  yield chatbot
96
 
 
 
 
 
 
 
 
 
 
 
 
97
  # Definir los componentes de entrada y salida
98
- chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False, scale=2, height=300)
99
- text_prompt_component = gr.Textbox(placeholder="Message...", show_label=False, autofocus=True, scale=8)
100
- upload_button_component = gr.UploadButton(label="Upload Images", file_count="multiple", file_types=["image"], scale=1)
 
 
 
 
 
 
 
 
 
101
  run_button_component = gr.Button(value="Run", variant="primary", scale=1)
102
  model_choice_component = gr.Dropdown(
103
  choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
@@ -105,29 +124,21 @@ model_choice_component = gr.Dropdown(
105
  label="Select Model",
106
  scale=2
107
  )
108
- use_system_instruction_component = gr.Dropdown(
109
- choices=["Yes", "No"],
110
- value="No",
111
- label="Use System Instruction",
112
  scale=1
113
  )
114
- system_instruction_component = gr.Textbox(
115
- placeholder="Enter system instruction...",
116
- show_label=True,
117
- scale=8,
118
- visible=False # Oculto inicialmente
119
- )
120
 
121
- # Funci贸n para actualizar la visibilidad del campo de instrucci贸n
122
- def toggle_system_instruction(use_system_instruction: str):
123
- return gr.update(visible=(use_system_instruction == "Yes"))
 
124
 
125
- user_inputs = [text_prompt_component, chatbot_component]
126
  bot_inputs = [
127
  upload_button_component,
128
- model_choice_component,
129
- use_system_instruction_component,
130
- system_instruction_component,
131
  chatbot_component
132
  ]
133
 
@@ -136,30 +147,28 @@ with gr.Blocks() as demo:
136
  gr.HTML(TITLE)
137
  gr.HTML(SUBTITLE)
138
  with gr.Column():
 
139
  model_choice_component.render()
140
  chatbot_component.render()
141
  with gr.Row():
142
  text_prompt_component.render()
143
  upload_button_component.render()
144
  run_button_component.render()
145
- use_system_instruction_component.render()
 
 
 
 
 
146
  system_instruction_component.render()
147
 
148
- use_system_instruction_component.change(
149
- fn=toggle_system_instruction,
150
- inputs=[use_system_instruction_component],
151
- outputs=[system_instruction_component]
152
- )
153
-
154
  run_button_component.click(
155
  fn=user,
156
  inputs=user_inputs,
157
  outputs=[text_prompt_component, chatbot_component],
158
  queue=False
159
  ).then(
160
- fn=bot,
161
- inputs=bot_inputs,
162
- outputs=[chatbot_component],
163
  )
164
 
165
  text_prompt_component.submit(
@@ -168,9 +177,7 @@ with gr.Blocks() as demo:
168
  outputs=[text_prompt_component, chatbot_component],
169
  queue=False
170
  ).then(
171
- fn=bot,
172
- inputs=bot_inputs,
173
- outputs=[chatbot_component],
174
  )
175
 
176
  upload_button_component.upload(
 
44
  image = Image.open(file).convert('RGB')
45
  image_preview = preprocess_image(image)
46
  if image_preview:
47
+ # Display a preview of the uploaded image
48
  gr.Image(image_preview).render()
49
  image_path = cache_pil_image(image)
50
  chatbot.append(((image_path,), None))
 
58
  def bot(
59
  files: Optional[List[str]],
60
  model_choice: str,
61
+ system_instruction: Optional[str], # Par谩metro para la instrucci贸n del sistema
 
62
  chatbot: CHAT_HISTORY
63
  ):
64
  if not GOOGLE_API_KEY:
65
  raise ValueError("GOOGLE_API_KEY is not set.")
66
 
67
+ # Configurar la API con la clave
68
  genai.configure(api_key=GOOGLE_API_KEY)
69
  generation_config = genai.types.GenerationConfig(
70
+ temperature=0.7, # Valor predeterminado
71
+ max_output_tokens=8192, # Fijar el l铆mite de tokens a 8,192
72
+ top_k=10, # Valor predeterminado
73
+ top_p=0.9 # Valor predeterminado
74
  )
75
 
76
  text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
77
  image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
78
+
79
+ # Crear el modelo con la instrucci贸n del sistema si est谩 definida
 
 
80
  model = genai.GenerativeModel(
81
  model_name=model_choice,
82
  generation_config=generation_config,
83
+ system_instruction=system_instruction if system_instruction else None
84
  )
85
 
86
  response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
 
93
  time.sleep(0.01)
94
  yield chatbot
95
 
96
+ # Componente para ingresar la instrucci贸n del sistema
97
+ system_instruction_component = gr.Textbox(
98
+ placeholder="Enter system instruction...",
99
+ show_label=True,
100
+ scale=8,
101
+ visible=False # Por defecto no visible
102
+ )
103
+
104
+ def toggle_system_instruction(system_instruction_active: bool):
105
+ return gr.update(visible=system_instruction_active)
106
+
107
  # Definir los componentes de entrada y salida
108
+ chatbot_component = gr.Chatbot(
109
+ label='Gemini',
110
+ bubble_full_width=False,
111
+ scale=2,
112
+ height=250
113
+ )
114
+ text_prompt_component = gr.Textbox(
115
+ placeholder="Message...", show_label=False, autofocus=True, scale=8
116
+ )
117
+ upload_button_component = gr.UploadButton(
118
+ label="Upload Images", file_count="multiple", file_types=["image"], scale=1
119
+ )
120
  run_button_component = gr.Button(value="Run", variant="primary", scale=1)
121
  model_choice_component = gr.Dropdown(
122
  choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
 
124
  label="Select Model",
125
  scale=2
126
  )
127
+ system_instruction_toggle = gr.Checkbox(
128
+ label="Enable System Instruction",
129
+ value=False,
 
130
  scale=1
131
  )
 
 
 
 
 
 
132
 
133
+ user_inputs = [
134
+ text_prompt_component,
135
+ chatbot_component
136
+ ]
137
 
 
138
  bot_inputs = [
139
  upload_button_component,
140
+ model_choice_component, # El campo de modelo est谩 ahora arriba
141
+ system_instruction_component, # Instrucci贸n del sistema sigue siendo separada
 
142
  chatbot_component
143
  ]
144
 
 
147
  gr.HTML(TITLE)
148
  gr.HTML(SUBTITLE)
149
  with gr.Column():
150
+ # Campo de selecci贸n de modelo arriba
151
  model_choice_component.render()
152
  chatbot_component.render()
153
  with gr.Row():
154
  text_prompt_component.render()
155
  upload_button_component.render()
156
  run_button_component.render()
157
+ system_instruction_toggle.render()
158
+ system_instruction_toggle.change(
159
+ fn=toggle_system_instruction,
160
+ inputs=[system_instruction_toggle],
161
+ outputs=[system_instruction_component]
162
+ )
163
  system_instruction_component.render()
164
 
 
 
 
 
 
 
165
  run_button_component.click(
166
  fn=user,
167
  inputs=user_inputs,
168
  outputs=[text_prompt_component, chatbot_component],
169
  queue=False
170
  ).then(
171
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
 
 
172
  )
173
 
174
  text_prompt_component.submit(
 
177
  outputs=[text_prompt_component, chatbot_component],
178
  queue=False
179
  ).then(
180
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
 
 
181
  )
182
 
183
  upload_button_component.upload(