Spaces:
Running
Running
File size: 3,579 Bytes
84e07f0 f981512 8d45e13 9a58086 84e07f0 f981512 5010813 f981512 56a81a0 81e1fd9 84e07f0 81e1fd9 84e07f0 99ca542 81e1fd9 62097fd 81e1fd9 84e07f0 62097fd 81e1fd9 9a58086 62097fd 9a58086 62097fd 81e1fd9 9a58086 84e07f0 0815875 84e07f0 81e1fd9 84e07f0 0815875 81e1fd9 0815875 9a58086 81e1fd9 0815875 9a58086 0815875 9a58086 84e07f0 62097fd 81e1fd9 62097fd 84e07f0 81e1fd9 62097fd 81e1fd9 84e07f0 62097fd 81e1fd9 62097fd 84e07f0 81e1fd9 84e07f0 62097fd 84e07f0 62097fd 84e07f0 81e1fd9 84e07f0 81e1fd9 84e07f0 81e1fd9 9a58086 84e07f0 62097fd 81e1fd9 9a58086 84e07f0 5010813 81e1fd9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
TITLE = """<h1 align="center">Gemini Playground ✨</h1>"""
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
import os
import time
from typing import Optional, Tuple
import google.generativeai as genai
import gradio as gr
from dotenv import load_dotenv
# Cargar las variables de entorno desde el archivo .env
load_dotenv()
print("google-generativeai:", genai.__version__)
# Obtener la clave de la API de las variables de entorno
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# Verificar que la clave de la API esté configurada
if not GOOGLE_API_KEY:
raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
# Configuración del modelo Gemini
genai.configure(api_key=GOOGLE_API_KEY)
generation_config = genai.types.GenerationConfig(
temperature=0.7,
max_output_tokens=8192,
top_k=10,
top_p=0.9
)
# Función para manejar las respuestas del modelo
def bot_response(
model_choice: str,
system_instruction: Optional[str],
text_prompt: str,
chatbot: list,
) -> Tuple[list, str]:
"""
Envía el mensaje al modelo y obtiene la respuesta, actualizando el historial del chatbot.
"""
if not text_prompt.strip():
return chatbot, "Por favor, escribe un mensaje válido."
# Configurar el modelo
model = genai.GenerativeModel(
model_name=model_choice,
generation_config=generation_config,
system_instruction=system_instruction or "You are an assistant."
)
# Generar la respuesta
response = model.generate_content([text_prompt], stream=True, generation_config=generation_config)
# Construir la respuesta completa
generated_text = ""
for chunk in response:
generated_text += chunk.text
# Actualizar el historial del chatbot con una sola entrada
chatbot.append((text_prompt, generated_text))
return chatbot, ""
# Componentes de la interfaz
chatbot_component = gr.Chatbot(label="Gemini", scale=2, height=300)
text_input_component = gr.Textbox(placeholder="Escribe un mensaje...", show_label=False, scale=8)
run_button_component = gr.Button(value="Enviar", variant="primary", scale=1)
model_dropdown_component = gr.Dropdown(
choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
value="gemini-1.5-flash",
label="Selecciona el modelo",
scale=2
)
system_instruction_component = gr.Textbox(
placeholder="Escribe una instrucción para el sistema...",
label="Instrucción del sistema",
scale=8,
value="You are an assistant."
)
# Definir la interfaz
with gr.Blocks() as demo:
gr.HTML(TITLE)
gr.HTML(SUBTITLE)
with gr.Column():
model_dropdown_component.render()
chatbot_component.render()
with gr.Row():
text_input_component.render()
run_button_component.render()
with gr.Accordion("Instrucción del sistema", open=False):
system_instruction_component.render()
# Configurar eventos
run_button_component.click(
fn=bot_response,
inputs=[model_dropdown_component, system_instruction_component, text_input_component, chatbot_component],
outputs=[chatbot_component, text_input_component],
)
text_input_component.submit(
fn=bot_response,
inputs=[model_dropdown_component, system_instruction_component, text_input_component, chatbot_component],
outputs=[chatbot_component, text_input_component],
)
# Lanzar la aplicación
if __name__ == "__main__":
demo.queue(max_size=99).launch(debug=True, show_error=True)
|