Spaces:
Runtime error
Runtime error
Ffftdtd5dtft
commited on
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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
|
10 |
+
model_urls = [
|
11 |
+
"https://huggingface.co/leejet/FLUX.1-schnell-gguf/resolve/main/flux1-schnell-q2_k.gguf",
|
12 |
+
"https://huggingface.co/aifoundry-org/FLUX.1-schnell-Quantized/resolve/main/flux1-schnell-Q2_K.gguf",
|
13 |
+
"https://huggingface.co/qwp4w3hyb/gemma-2-27b-it-iMat-GGUF/resolve/main/gemma-2-27b-it-imat-IQ1_S.gguf",
|
14 |
+
"https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q2_K.gguf",
|
15 |
+
"https://huggingface.co/WongBingbing/Meta-Llama-3.1-8B-Instruct-Q2_K-GGUF/resolve/main/meta-llama-3.1-8b-instruct-q2_k.gguf",
|
16 |
+
"https://huggingface.co/city96/FLUX.1-schnell-gguf/resolve/main/flux1-schnell-Q2_K.gguf",
|
17 |
+
"https://huggingface.co/mradermacher/L3-Super-Nova-RP-8B-i1-GGUF/resolve/main/L3-Super-Nova-RP-8B.i1-IQ1_M.gguf",
|
18 |
+
"https://huggingface.co/zhhan/Phi-3-mini-4k-instruct_gguf_derived/resolve/main/Phi-3-mini-4k-instruct-q4.gguf"
|
19 |
+
]
|
20 |
+
|
21 |
+
# Nombres de los archivos descargados
|
22 |
+
model_files = [
|
23 |
+
"flux1-schnell-q2_k.gguf",
|
24 |
+
"flux1-schnell-Q2_K.gguf",
|
25 |
+
"gemma-2-27b-it-imat-IQ1_S.gguf",
|
26 |
+
"llama-2-7b-chat.Q2_K.gguf",
|
27 |
+
"meta-llama-3.1-8b-instruct-q2_k.gguf",
|
28 |
+
"flux1-schnell-Q2_K.gguf",
|
29 |
+
"L3-Super-Nova-RP-8B.i1-IQ1_M.gguf",
|
30 |
+
"Phi-3-mini-4k-instruct-q4.gguf"
|
31 |
+
]
|
32 |
+
|
33 |
+
# Función para descargar los modelos utilizando wget
|
34 |
+
def download_models(model_urls, model_files):
|
35 |
+
for url, file in zip(model_urls, model_files):
|
36 |
+
if not os.path.exists(file):
|
37 |
+
wget.download(url, out=file)
|
38 |
+
|
39 |
+
# Inicializar el modelo de transcripción Whisper
|
40 |
+
def initialize_whisper():
|
41 |
+
model = whisper.load_model("base")
|
42 |
+
return model
|
43 |
+
|
44 |
+
# Inicializa el chatbot
|
45 |
+
def initialize_chatbot(model_files):
|
46 |
+
model_path = random.choice(model_files) # Selecciona un modelo aleatorio
|
47 |
+
chatbot = GPT4All(model_path=model_path)
|
48 |
+
return chatbot, model_path
|
49 |
+
|
50 |
+
# Función para la generación de canciones con MusicGen
|
51 |
+
def generate_song(prompt, model_type="standard"):
|
52 |
+
if model_type == "medium":
|
53 |
+
model = MusicGen.get_pretrained("musicgen-medium")
|
54 |
+
else:
|
55 |
+
model = MusicGen.get_pretrained("melody")
|
56 |
+
|
57 |
+
model.set_generation_params(duration=30) # Duración de la canción en segundos
|
58 |
+
wav_output = model.generate(prompt)
|
59 |
+
song_path = "generated_song.wav"
|
60 |
+
model.save_wav(wav_output, song_path)
|
61 |
+
return song_path
|
62 |
+
|
63 |
+
# Función para transcribir audio con Whisper
|
64 |
+
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, chatbot, model_path, whisper_model=None, audio_path=None):
|
70 |
+
if user_input.lower() == "salir":
|
71 |
+
return "Conexión terminada."
|
72 |
+
|
73 |
+
# Verificar si el modelo es flux1-schnell y debe generar una imagen
|
74 |
+
if "flux1-schnell" in model_path.lower():
|
75 |
+
if "imagen" in user_input.lower():
|
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 chatbot.chat(user_input)
|
89 |
+
|
90 |
+
# Crear la interfaz de Gradio
|
91 |
+
def create_gradio_interface(chatbot, model_path, whisper_model):
|
92 |
+
def gradio_chat(user_input, audio_input=None):
|
93 |
+
response = chatbot_response(user_input, chatbot, model_path, whisper_model, audio_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"):
|
97 |
+
return None, None, response, None # Devuelve None en el texto, ninguna imagen, y la canción
|
98 |
+
else:
|
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 GPT4All con Imágenes, Canciones, y Transcripción de Audio")
|
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 |
+
chatbot, model_path = initialize_chatbot(model_files)
|
109 |
+
whisper_model = initialize_whisper() # Inicializar el modelo de Whisper
|
110 |
+
iface = create_gradio_interface(chatbot, model_path, whisper_model)
|
111 |
+
iface.launch()
|
112 |
+
|
113 |
+
if __name__ == "__main__":
|
114 |
+
run_chatbot_with_gradio()
|