Spaces:
Running
Running
| import gradio as gr | |
| import openai | |
| from transformers import pipeline | |
| import base64 | |
| import os | |
| from dotenv import load_dotenv | |
| # Carga variables de entorno de archivo .env | |
| load_dotenv() | |
| # Configuraci贸n de clave API OpenAI | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| # Modelos de texto OpenAI disponibles | |
| MODELOS_OPENAI = ["gpt-3.5-turbo", "gpt-4-turbo", "gpt-4o", "gpt-5"] | |
| # Configurar pipeline Whisper para reconocimiento y detecci贸n de idioma | |
| asr = pipeline("automatic-speech-recognition", model="openai/whisper-large") | |
| # Preset para voz sensual Maya1 (ajustar seg煤n modelo real) | |
| PRESET_CHARACTERS = { | |
| "Female Sexy": { | |
| "description": "Voz femenina sensual, ritmo pausado y c谩lido tono seductor.", | |
| "example_text": "Hola, 驴c贸mo puedo hacer que tu d铆a sea a煤n m谩s especial?" | |
| } | |
| } | |
| # Funci贸n simulada para s铆ntesis audio Maya1 (reemplaza esta funci贸n por integraci贸n real) | |
| def generate_speech_maya1(text, preset="Female Sexy"): | |
| # Simula retorno de archivo de audio generado | |
| return "audio_generado.wav" | |
| # Funci贸n para transcribir y detectar idioma del audio | |
| def transcribe_and_detect(audio): | |
| result = asr(audio) | |
| texto = result.get("text", "") | |
| idioma = result.get("language", "es") | |
| return texto, idioma | |
| # Genera texto de respuesta en espa帽ol y tono seductor | |
| def generar_respuesta(mensaje, modelo): | |
| prompt = f"Responde en espa帽ol con voz sensual y seductora: {mensaje}" | |
| response = openai.ChatCompletion.create( | |
| model=modelo, | |
| messages=[{"role": "user", "content": prompt}] | |
| ) | |
| return response.choices[0].message.content | |
| # Generar imagen con GPT-5 y OpenAI Responses API | |
| def generar_imagen(prompt): | |
| response = openai.responses.create( | |
| model="gpt-5", | |
| input=prompt, | |
| tools=[{"type": "image_generation"}], | |
| ) | |
| image_data = [output.result for output in response.output if output.type == "image_generation_call"] | |
| if image_data: | |
| img_b64 = image_data[0] | |
| img_bytes = base64.b64decode(img_b64) | |
| path = "imagen_generada.png" | |
| with open(path, "wb") as f: | |
| f.write(img_bytes) | |
| return path | |
| return None | |
| # Funci贸n principal que integra audio, texto, voz e imagen | |
| def chatbot_completo(audio, modelo_texto, preset_voz, prompt_imagen): | |
| texto_usuario, _ = transcribe_and_detect(audio) | |
| texto_respuesta = generar_respuesta(texto_usuario, modelo_texto) | |
| audio_path = generate_speech_maya1(texto_respuesta, preset_voz) | |
| image_path = generar_imagen(prompt_imagen) if prompt_imagen.strip() else None | |
| return texto_usuario, texto_respuesta, audio_path, image_path | |
| # Construcci贸n interfaz Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Chatbot Multimodal Avanzado\nDetecta idioma, responde con voz sensual y genera im谩genes") | |
| with gr.Row(): | |
| with gr.Column(): | |
| audio_in = gr.Audio(type="filepath", label="Habla aqu铆") | |
| modelo_sel = gr.Dropdown(MODELOS_OPENAI, value="gpt-4-turbo", label="Modelo de texto") | |
| preset_sel = gr.Dropdown(list(PRESET_CHARACTERS.keys()), value="Female Sexy", label="Preset de voz sensual") | |
| text_img = gr.Textbox(label="Texto para generar imagen (opcional)", lines=2) | |
| btn = gr.Button("Enviar") | |
| with gr.Column(): | |
| text_trans = gr.Textbox(label="Texto reconocido") | |
| text_resp = gr.Textbox(label="Respuesta del chatbot") | |
| audio_resp = gr.Audio(type="filepath", label="Respuesta de voz") | |
| image_resp = gr.Image(label="Imagen generada (opcional)") | |
| btn.click( | |
| chatbot_completo, | |
| inputs=[audio_in, modelo_sel, preset_sel, text_img], | |
| outputs=[text_trans, text_resp, audio_resp, image_resp] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |