JeCabrera commited on
Commit
aef709e
verified
1 Parent(s): effb607

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -14,8 +14,12 @@ from dotenv import load_dotenv
14
  # Cargar las variables de entorno desde el archivo .env
15
  load_dotenv()
16
 
17
- # Verificar la clave de API
 
 
18
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
 
 
19
  if not GOOGLE_API_KEY:
20
  raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
21
 
@@ -54,7 +58,7 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
54
  def bot(
55
  files: Optional[List[str]],
56
  model_choice: str,
57
- system_instruction: str, # Instrucci贸n del sistema extra铆da directamente
58
  chatbot: CHAT_HISTORY
59
  ):
60
  if not GOOGLE_API_KEY:
@@ -63,20 +67,20 @@ def bot(
63
  # Configurar la API con la clave
64
  genai.configure(api_key=GOOGLE_API_KEY)
65
  generation_config = genai.types.GenerationConfig(
66
- temperature=0.7,
67
- max_output_tokens=8192,
68
- top_k=10,
69
- top_p=0.9
70
  )
71
 
72
  text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
73
  image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
74
-
75
- # Usar la instrucci贸n del sistema si est谩 presente
76
  model = genai.GenerativeModel(
77
  model_name=model_choice,
78
  generation_config=generation_config,
79
- system_instruction=system_instruction if system_instruction else None
80
  )
81
 
82
  response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
@@ -89,18 +93,15 @@ def bot(
89
  time.sleep(0.01)
90
  yield chatbot
91
 
92
- # Componente para la instrucci贸n del sistema dentro de un desplegable
93
- system_instruction_dropdown = gr.Dropdown(
94
- choices=[
95
- "Keep responses concise and professional.",
96
- "Use a friendly and engaging tone.",
97
- "Focus on technical explanations.",
98
- "Encourage creative ideas.",
99
- "Simplify complex concepts for a beginner audience."
100
- ],
101
  label="System Instruction",
102
- placeholder="Select or leave empty",
103
- scale=2
 
 
 
 
104
  )
105
 
106
  # Definir los componentes de entrada y salida
@@ -131,8 +132,8 @@ user_inputs = [
131
 
132
  bot_inputs = [
133
  upload_button_component,
134
- model_choice_component,
135
- system_instruction_dropdown, # Desplegable de System Instruction
136
  chatbot_component
137
  ]
138
 
@@ -147,7 +148,7 @@ with gr.Blocks() as demo:
147
  text_prompt_component.render()
148
  upload_button_component.render()
149
  run_button_component.render()
150
- system_instruction_dropdown.render() # Agregar desplegable para System Instruction
151
 
152
  run_button_component.click(
153
  fn=user,
 
14
  # Cargar las variables de entorno desde el archivo .env
15
  load_dotenv()
16
 
17
+ print("google-generativeai:", genai.__version__)
18
+
19
+ # Obtener la clave de la API de las variables de entorno
20
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
21
+
22
+ # Verificar que la clave de la API est茅 configurada
23
  if not GOOGLE_API_KEY:
24
  raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
25
 
 
58
  def bot(
59
  files: Optional[List[str]],
60
  model_choice: str,
61
+ system_instruction: str, # Instrucci贸n del sistema
62
  chatbot: CHAT_HISTORY
63
  ):
64
  if not GOOGLE_API_KEY:
 
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
80
  model = genai.GenerativeModel(
81
  model_name=model_choice,
82
  generation_config=generation_config,
83
+ system_instruction=system_instruction # Se pasa la instrucci贸n del sistema
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 dentro del despegable
97
+ system_instruction_dropdown = gr.Accordion(
 
 
 
 
 
 
 
98
  label="System Instruction",
99
+ open=False,
100
+ children=gr.Textbox(
101
+ lines=2,
102
+ show_label=False,
103
+ placeholder="Enter system instruction here..."
104
+ )
105
  )
106
 
107
  # Definir los componentes de entrada y salida
 
132
 
133
  bot_inputs = [
134
  upload_button_component,
135
+ model_choice_component, # El campo de modelo est谩 ahora arriba
136
+ system_instruction_dropdown.children, # Se toma el texto del componente interno
137
  chatbot_component
138
  ]
139
 
 
148
  text_prompt_component.render()
149
  upload_button_component.render()
150
  run_button_component.render()
151
+ system_instruction_dropdown.render()
152
 
153
  run_button_component.click(
154
  fn=user,