copywriter / app.py
JeCabrera's picture
Update app.py
ae628c2 verified
raw
history blame
2.34 kB
import os
import time
import gradio as gr
import google.generativeai as genai
from typing import Optional, List
# Cargar la clave API desde el entorno
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if not GOOGLE_API_KEY:
raise ValueError("GOOGLE_API_KEY is not set.")
# Configurar la API de Gemini
genai.configure(api_key=GOOGLE_API_KEY)
def transform_history(history):
"""Transforma el historial en el formato esperado por Gemini."""
new_history = []
for chat in history:
new_history.append({"role": "user", "content": chat[0]})
new_history.append({"role": "assistant", "content": chat[1]})
return new_history
def bot(files: Optional[List[str]], model_choice: str, system_instruction: Optional[str], history):
"""Procesa la interacci贸n del chatbot."""
chat_history = transform_history(history)
if system_instruction:
chat_history.insert(0, {"role": "system", "content": system_instruction})
# Configuraci贸n del modelo Flash 1.5
generation_config = genai.types.GenerationConfig(
temperature=0.7,
max_output_tokens=8192,
top_k=10,
top_p=0.9
)
response = genai.ChatCompletion.create(
model=model_choice,
messages=chat_history + [{"role": "user", "content": history[-1][0]}],
generation_config=generation_config
)
reply = response['candidates'][0]['content']
for i in range(len(reply)):
time.sleep(0.05)
yield history + [{"role": "assistant", "content": reply[:i + 1]}]
# Interfaz con Gradio
with gr.Blocks() as demo:
chatbot = gr.Chatbot(elem_id="chatbot", bubble_full_width=False, type="messages")
chat_input = gr.Textbox(
placeholder="Escribe un mensaje...",
show_label=False
)
submit_btn = gr.Button("Enviar")
system_input = gr.Textbox(placeholder="Instrucci贸n del sistema (opcional)", show_label=True, lines=2)
model_choice = gr.Dropdown(choices=["gemini-1.5-flash"], value="gemini-1.5-flash", label="Modelo")
submit_btn.click(
bot, # Funci贸n que manejar谩 la acci贸n del bot贸n
inputs=[chat_input, model_choice, system_input, chatbot], # Aseg煤rate de que las entradas sean v谩lidas
outputs=chatbot # El chatbot ser谩 actualizado con la respuesta
)
demo.launch()