Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,12 +14,8 @@ from dotenv import load_dotenv
|
|
14 |
# Cargar las variables de entorno desde el archivo .env
|
15 |
load_dotenv()
|
16 |
|
17 |
-
|
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,7 +54,7 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
58 |
def bot(
|
59 |
files: Optional[List[str]],
|
60 |
model_choice: str,
|
61 |
-
system_instruction:
|
62 |
chatbot: CHAT_HISTORY
|
63 |
):
|
64 |
if not GOOGLE_API_KEY:
|
@@ -67,16 +63,16 @@ def bot(
|
|
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,
|
71 |
-
max_output_tokens=8192,
|
72 |
-
top_k=10,
|
73 |
-
top_p=0.9
|
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 |
-
#
|
80 |
model = genai.GenerativeModel(
|
81 |
model_name=model_choice,
|
82 |
generation_config=generation_config,
|
@@ -93,17 +89,20 @@ def bot(
|
|
93 |
time.sleep(0.01)
|
94 |
yield chatbot
|
95 |
|
96 |
-
# Componente para
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
)
|
103 |
|
104 |
-
def toggle_system_instruction(option: str):
|
105 |
-
return gr.update(visible=option == "Enable")
|
106 |
-
|
107 |
# Definir los componentes de entrada y salida
|
108 |
chatbot_component = gr.Chatbot(
|
109 |
label='Gemini',
|
@@ -124,12 +123,6 @@ model_choice_component = gr.Dropdown(
|
|
124 |
label="Select Model",
|
125 |
scale=2
|
126 |
)
|
127 |
-
system_instruction_toggle = gr.Dropdown(
|
128 |
-
label="System Instruction",
|
129 |
-
choices=["Disable", "Enable"],
|
130 |
-
value="Disable",
|
131 |
-
scale=1
|
132 |
-
)
|
133 |
|
134 |
user_inputs = [
|
135 |
text_prompt_component,
|
@@ -138,8 +131,8 @@ user_inputs = [
|
|
138 |
|
139 |
bot_inputs = [
|
140 |
upload_button_component,
|
141 |
-
model_choice_component,
|
142 |
-
|
143 |
chatbot_component
|
144 |
]
|
145 |
|
@@ -148,20 +141,13 @@ with gr.Blocks() as demo:
|
|
148 |
gr.HTML(TITLE)
|
149 |
gr.HTML(SUBTITLE)
|
150 |
with gr.Column():
|
151 |
-
# Campo de selecci贸n de modelo arriba
|
152 |
model_choice_component.render()
|
153 |
chatbot_component.render()
|
154 |
with gr.Row():
|
155 |
text_prompt_component.render()
|
156 |
upload_button_component.render()
|
157 |
run_button_component.render()
|
158 |
-
|
159 |
-
system_instruction_toggle.change(
|
160 |
-
fn=toggle_system_instruction,
|
161 |
-
inputs=[system_instruction_toggle],
|
162 |
-
outputs=[system_instruction_component]
|
163 |
-
)
|
164 |
-
system_instruction_component.render()
|
165 |
|
166 |
run_button_component.click(
|
167 |
fn=user,
|
|
|
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 |
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 |
# 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,
|
|
|
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
|
107 |
chatbot_component = gr.Chatbot(
|
108 |
label='Gemini',
|
|
|
123 |
label="Select Model",
|
124 |
scale=2
|
125 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
user_inputs = [
|
128 |
text_prompt_component,
|
|
|
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 |
|
|
|
141 |
gr.HTML(TITLE)
|
142 |
gr.HTML(SUBTITLE)
|
143 |
with gr.Column():
|
|
|
144 |
model_choice_component.render()
|
145 |
chatbot_component.render()
|
146 |
with gr.Row():
|
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,
|