Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import os
|
|
| 2 |
import random
|
| 3 |
import gradio as gr
|
| 4 |
import wget
|
|
|
|
| 5 |
import whisper
|
| 6 |
-
from gpt4all import GPT4All
|
| 7 |
from audiocraft.models import MusicGen
|
| 8 |
|
| 9 |
# URLs de los modelos a descargar
|
|
@@ -41,11 +41,15 @@ def initialize_whisper():
|
|
| 41 |
model = whisper.load_model("base")
|
| 42 |
return model
|
| 43 |
|
| 44 |
-
# Inicializa
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Funci贸n para la generaci贸n de canciones con MusicGen
|
| 51 |
def generate_song(prompt, model_type="standard"):
|
|
@@ -65,32 +69,42 @@ def transcribe_audio(audio_path, whisper_model):
|
|
| 65 |
transcription = whisper_model.transcribe(audio_path)
|
| 66 |
return transcription["text"]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Funci贸n para el chatbot con Gradio
|
| 69 |
-
def chatbot_response(user_input,
|
| 70 |
if user_input.lower() == "salir":
|
| 71 |
return "Conexi贸n terminada."
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
image_path = "output_image.png"
|
| 77 |
-
chatbot.generate_image(user_input, output=image_path) # Asumiendo que el chatbot tiene este m茅todo
|
| 78 |
-
return image_path # Devuelve la ruta de la imagen generada
|
| 79 |
-
else:
|
| 80 |
-
return chatbot.chat(user_input)
|
| 81 |
elif "canci贸n" in user_input.lower() or "musica" in user_input.lower():
|
| 82 |
model_type = "medium" if "medium" in user_input.lower() else "standard"
|
| 83 |
song_path = generate_song(user_input, model_type=model_type)
|
| 84 |
return song_path # Devuelve la ruta de la canci贸n generada
|
|
|
|
| 85 |
elif audio_path: # Si se proporciona un archivo de audio, transcribirlo
|
| 86 |
return transcribe_audio(audio_path, whisper_model)
|
|
|
|
| 87 |
else:
|
| 88 |
-
return
|
| 89 |
|
| 90 |
# Crear la interfaz de Gradio
|
| 91 |
-
def create_gradio_interface(
|
| 92 |
def gradio_chat(user_input, audio_input=None):
|
| 93 |
-
response = chatbot_response(user_input,
|
| 94 |
if isinstance(response, str) and response.endswith(".png"):
|
| 95 |
return None, response, None, None # Devuelve None en el texto y la imagen, y ninguna canci贸n
|
| 96 |
elif isinstance(response, str) and response.endswith(".wav"):
|
|
@@ -99,15 +113,15 @@ def create_gradio_interface(chatbot, model_path, whisper_model):
|
|
| 99 |
return response, None, None, None # Devuelve el texto, ninguna imagen, ninguna canci贸n, y ninguna transcripci贸n
|
| 100 |
|
| 101 |
# Crear interfaz con un input y cuatro outputs (texto, imagen, canci贸n, y transcripci贸n)
|
| 102 |
-
iface = gr.Interface(fn=gradio_chat, inputs=["text", "audio"], outputs=["text", "image", "audio", "text"], title="Chatbot
|
| 103 |
return iface
|
| 104 |
|
| 105 |
# Ejecuta el chatbot con Gradio
|
| 106 |
def run_chatbot_with_gradio():
|
| 107 |
download_models(model_urls, model_files) # Descargar los modelos si no est谩n presentes
|
| 108 |
-
|
| 109 |
whisper_model = initialize_whisper() # Inicializar el modelo de Whisper
|
| 110 |
-
iface = create_gradio_interface(
|
| 111 |
iface.launch()
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import random
|
| 3 |
import gradio as gr
|
| 4 |
import wget
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 6 |
import whisper
|
|
|
|
| 7 |
from audiocraft.models import MusicGen
|
| 8 |
|
| 9 |
# URLs de los modelos a descargar
|
|
|
|
| 41 |
model = whisper.load_model("base")
|
| 42 |
return model
|
| 43 |
|
| 44 |
+
# Inicializa los modelos de transformers
|
| 45 |
+
def initialize_transformer_models():
|
| 46 |
+
model_names = ["gpt2", "gpt2-medium", "gpt2-large"] # Puedes agregar m谩s modelos
|
| 47 |
+
models = []
|
| 48 |
+
for model_name in model_names:
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 50 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 51 |
+
models.append((model, tokenizer))
|
| 52 |
+
return models
|
| 53 |
|
| 54 |
# Funci贸n para la generaci贸n de canciones con MusicGen
|
| 55 |
def generate_song(prompt, model_type="standard"):
|
|
|
|
| 69 |
transcription = whisper_model.transcribe(audio_path)
|
| 70 |
return transcription["text"]
|
| 71 |
|
| 72 |
+
# Funci贸n para unificar las respuestas de diferentes modelos
|
| 73 |
+
def unified_response(user_input, models):
|
| 74 |
+
responses = []
|
| 75 |
+
for model, tokenizer in models:
|
| 76 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 77 |
+
outputs = model.generate(**inputs)
|
| 78 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 79 |
+
responses.append(response)
|
| 80 |
+
|
| 81 |
+
# Unificar respuestas (puedes aplicar m谩s l贸gica aqu铆, como seleccionar la m谩s com煤n)
|
| 82 |
+
final_response = random.choice(responses)
|
| 83 |
+
return final_response
|
| 84 |
+
|
| 85 |
# Funci贸n para el chatbot con Gradio
|
| 86 |
+
def chatbot_response(user_input, models, whisper_model=None, audio_path=None):
|
| 87 |
if user_input.lower() == "salir":
|
| 88 |
return "Conexi贸n terminada."
|
| 89 |
|
| 90 |
+
if "imagen" in user_input.lower():
|
| 91 |
+
return "Funcionalidad de generaci贸n de im谩genes no soportada por estos modelos."
|
| 92 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
elif "canci贸n" in user_input.lower() or "musica" in user_input.lower():
|
| 94 |
model_type = "medium" if "medium" in user_input.lower() else "standard"
|
| 95 |
song_path = generate_song(user_input, model_type=model_type)
|
| 96 |
return song_path # Devuelve la ruta de la canci贸n generada
|
| 97 |
+
|
| 98 |
elif audio_path: # Si se proporciona un archivo de audio, transcribirlo
|
| 99 |
return transcribe_audio(audio_path, whisper_model)
|
| 100 |
+
|
| 101 |
else:
|
| 102 |
+
return unified_response(user_input, models)
|
| 103 |
|
| 104 |
# Crear la interfaz de Gradio
|
| 105 |
+
def create_gradio_interface(models, whisper_model):
|
| 106 |
def gradio_chat(user_input, audio_input=None):
|
| 107 |
+
response = chatbot_response(user_input, models, whisper_model, audio_input)
|
| 108 |
if isinstance(response, str) and response.endswith(".png"):
|
| 109 |
return None, response, None, None # Devuelve None en el texto y la imagen, y ninguna canci贸n
|
| 110 |
elif isinstance(response, str) and response.endswith(".wav"):
|
|
|
|
| 113 |
return response, None, None, None # Devuelve el texto, ninguna imagen, ninguna canci贸n, y ninguna transcripci贸n
|
| 114 |
|
| 115 |
# Crear interfaz con un input y cuatro outputs (texto, imagen, canci贸n, y transcripci贸n)
|
| 116 |
+
iface = gr.Interface(fn=gradio_chat, inputs=["text", "audio"], outputs=["text", "image", "audio", "text"], title="Chatbot con Im谩genes, Canciones, y Transcripci贸n de Audio")
|
| 117 |
return iface
|
| 118 |
|
| 119 |
# Ejecuta el chatbot con Gradio
|
| 120 |
def run_chatbot_with_gradio():
|
| 121 |
download_models(model_urls, model_files) # Descargar los modelos si no est谩n presentes
|
| 122 |
+
models = initialize_transformer_models() # Inicializar modelos de Transformers
|
| 123 |
whisper_model = initialize_whisper() # Inicializar el modelo de Whisper
|
| 124 |
+
iface = create_gradio_interface(models, whisper_model)
|
| 125 |
iface.launch()
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|