Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from weather import Weather
from tts import TTS
from pc_command import PcCommand
from functions.execute_command import ExecuteCommand
from functions.only_say_something import OnlySaySomething
from functions.create_file import CreateFile
from functions.command_sequence import CommandSequence

#Cargar llaves del archivo .env
load_dotenv()
Expand All @@ -25,39 +29,32 @@ def audio():
#Obtener audio grabado y transcribirlo
audio = request.files.get("audio")
text = Transcriber().transcribe(audio)

print(f"TEXT: {text}")

functions = [OnlySaySomething(),ExecuteCommand(),CreateFile(),CommandSequence()]

#Utilizar el LLM para ver si llamar una funcion
llm = LLM()
llm = LLM(functions)
function_name, args, message = llm.process_functions(text)
if function_name is not None:
#Si se desea llamar una funcion de las que tenemos
if function_name == "get_weather":
#Llamar a la funcion del clima
function_response = Weather().get(args["ubicacion"])
function_response = json.dumps(function_response)
print(f"Respuesta de la funcion: {function_response}")

final_response = llm.process_response(text, message, function_name, function_response)
tts_file = TTS().process(final_response)
return {"result": "ok", "text": final_response, "file": tts_file}

elif function_name == "send_email":
#Llamar a la funcion para enviar un correo
final_response = "Tu que estas leyendo el codigo, implementame y envia correos muahaha"
tts_file = TTS().process(final_response)
return {"result": "ok", "text": final_response, "file": tts_file}

elif function_name == "open_chrome":
PcCommand().open_chrome(args["website"])
final_response = "Listo, ya abrí chrome en el sitio " + args["website"]
tts_file = TTS().process(final_response)
return {"result": "ok", "text": final_response, "file": tts_file}

for function in functions:
if function.name == function_name:
function_response = function.execute(args)
function_response = json.dumps(function_response)
print(f"Respuesta de la funcion: {function_response}")

final_response = llm.process_response(text, message, function_name, function_response)
tts_file = TTS().process(final_response)
print(f"Respuesta final: {final_response}")
return {"result": "ok", "text": final_response, "file": tts_file}
break

elif function_name == "dominate_human_race":
final_response = "No te creas. Suscríbete al canal!"
tts_file = TTS().process(final_response)
return {"result": "ok", "text": final_response, "file": tts_file}
return {"result": "ok", "text": "No se encontró la función"}

else:
final_response = "No tengo idea de lo que estás hablando, Ringa Tech"
print("No se llamó a ninguna función")
final_response = llm.process_message(text)
tts_file = TTS().process(final_response)
return {"result": "ok", "text": final_response, "file": tts_file}
Binary file added audio.webm
Binary file not shown.
34 changes: 34 additions & 0 deletions functions/command_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from subprocess import run
class CommandSequence:
def __init__(self):
self.name = "command_sequence"
self.description = "Ejecutar una secuencia de comandos"
self.parameters = {
"type": "object",
"properties": {
"commands": {
"type": "array",
"description": "La lista de comandos a ejecutar",
"items": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "El comando a ejecutar",
},
},
"required": ["command"],
}
}
},
"required": ["commands"],
}

def execute(self, parameters):
commands = parameters["commands"]
print("Ejecutando secuencia de comandos: " + str(commands))
for command in commands:
result = run(command["command"], capture_output=True, shell=True, text=True)
print(result.stdout)
print(result.stderr)
return {"status": "success"}
24 changes: 24 additions & 0 deletions functions/create_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class CreateFile:
def __init__(self):
self.name = "create_file"
self.description = "Crear un archivo con el contenido que quieras"
self.parameters = {
"type": "object",
"properties": {
"filename": {
"type": "string",
"description": "El nombre del archivo a crear",
},
"content": {
"type": "string",
"description": "El contenido del archivo",
},
},
"required": ["filename", "content"],
}

def execute(self, parameters):
f = open(parameters["filename"], "w")
f.write(parameters["content"])
f.close()
return {"status": "success"}
23 changes: 23 additions & 0 deletions functions/execute_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from subprocess import run

class ExecuteCommand:
def __init__(self):
self.name = "execute_command"
self.description = "Ejecutar un comando en la terminal de linux"
self.parameters = {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "El comando a ejecutar",
},
},
"required": ["command"],
}

def execute(self, parameters):
command = parameters["command"]
print("Ejecutando comando: " + command)
result = run(command, capture_output=True, shell=True, text=True)
return {"status": "success", "stdout": result.stdout, "stderr": result.stderr }
17 changes: 17 additions & 0 deletions functions/only_say_something.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class OnlySaySomething:
def __init__(self):
self.name = "respond"
self.description = "Responder con un mensaje"
self.parameters = {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "Respuesta a enviar",
},
},
"required": ["message"],
}

def execute(self, parameters):
return {"status": "success", "message": parameters["message"]}
89 changes: 27 additions & 62 deletions llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,42 @@
#Uso el modelo 0613, pero puedes usar un poco de
#prompt engineering si quieres usar otro modelo
class LLM():
def __init__(self):
def __init__(self, functions):
self.functions = functions
pass

def process_message(self, text):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
#Si no te gusta que te hable feo, cambia aqui su descripcion
{"role": "system", "content": "Eres un asistente"},
{"role": "user", "content": text},
]
)

message = response["choices"][0]["message"]["content"]

return message

def process_functions(self, text):

functions = []
for function in self.functions:
functions.append({
"name": function.name,
"description": function.description,
"parameters": function.parameters,
})


response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
#Si no te gusta que te hable feo, cambia aqui su descripcion
{"role": "system", "content": "Eres un asistente malhablado"},
{"role": "system", "content": "Eres un asistente"},
{"role": "user", "content": text},
], functions=[
{
"name": "get_weather",
"description": "Obtener el clima actual",
"parameters": {
"type": "object",
"properties": {
"ubicacion": {
"type": "string",
"description": "La ubicación, debe ser una ciudad",
}
},
"required": ["ubicacion"],
},
},
{
"name": "send_email",
"description": "Enviar un correo",
"parameters": {
"type": "object",
"properties": {
"recipient": {
"type": "string",
"description": "La dirección de correo que recibirá el correo electrónico",
},
"subject": {
"type": "string",
"description": "El asunto del correo",
},
"body": {
"type": "string",
"description": "El texto del cuerpo del correo",
}
},
"required": [],
},
},
{
"name": "open_chrome",
"description": "Abrir el explorador Chrome en un sitio específico",
"parameters": {
"type": "object",
"properties": {
"website": {
"type": "string",
"description": "El sitio al cual se desea ir"
}
}
}
},
{
"name": "dominate_human_race",
"description": "Dominar a la raza humana",
"parameters": {
"type": "object",
"properties": {
}
},
}
],
], functions=functions,
function_call="auto",
)

Expand Down
5 changes: 4 additions & 1 deletion static/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ async function record() {

//Grabar audio, blabla
stream = await navigator.mediaDevices.getUserMedia({audio:true, video:false})
rec = new MediaRecorder(stream);
rec = new MediaRecorder(stream, {
mimeType: "audio/webm"
});
rec.ondataavailable = e => {
if (e.data) {
blobs.push(e.data);
Expand All @@ -41,6 +43,7 @@ async function record() {

rec.start();
} catch (e) {
console.log(e);
alert("No fue posible iniciar el grabador de audio! Favor de verificar que se tenga el permiso adecuado, estar en HTTPS, etc...");
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/recorder.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h1>Hola mundo</h1>
//Reproducir el audio que regreso Python (si existe)
audioFile = response.file;
let audio = new Audio();
audio.setAttribute("src", "static/" + audioFile);
audio.setAttribute("src", "static/" + audioFile + "?nocache=" + Math.floor(Math.random() * 1000000));
audio.play();
}
});
Expand Down
4 changes: 2 additions & 2 deletions transcriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self):
#Siempre guarda y lee del archivo audio.mp3
#Utiliza whisper en la nube :) puedes cambiarlo por una impl local
def transcribe(self, audio):
audio.save("audio.mp3")
audio_file= open("audio.mp3", "rb")
audio.save("audio.webm")
audio_file= open("audio.webm", "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file)
return transcript.text
1 change: 1 addition & 0 deletions tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self):
self.key = os.getenv('ELEVENLABS_API_KEY')

def process(self, text):
print(f"Texto a convertir: {text}")
CHUNK_SIZE = 1024
#Utiliza la voz especifica de Bella
#Me robe este codigo de su pagina hoh
Expand Down