jcanueto commited on
Commit
54d9a57
·
verified ·
1 Parent(s): af8e006

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from openai import OpenAI
3
+ import gradio as gr
4
+ import os
5
+ import dotenv
6
+
7
+
8
+
9
+ api_key = os.getenv("OPENROUTER_API_KEY")
10
+
11
+ client = OpenAI(
12
+ base_url="https://openrouter.ai/api/v1",
13
+ api_key=api_key,
14
+ )
15
+
16
+
17
+ asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
18
+
19
+
20
+ def speech_to_text(speech):
21
+ text = asr(speech)["text"]
22
+ text = text.lower()
23
+ return text
24
+
25
+ def chatbot(message, history):
26
+ messages = []
27
+ for human, assistant in history:
28
+ messages.append({"role": "user", "content": human})
29
+ messages.append({"role": "assistant", "content": assistant})
30
+ messages.append({"role": "user", "content": message})
31
+
32
+ response = client.chat.completions.create(
33
+ model="meta-llama/llama-3.1-405b-instruct:free",
34
+ messages=messages,
35
+ temperature=0.7,
36
+ max_tokens=1000
37
+ )
38
+ return response.choices[0].message.content
39
+
40
+
41
+ with gr.Blocks() as demo:
42
+ with gr.Row():
43
+ audio_file = gr.Audio(type="filepath", label="Sube tu archivo de audio")
44
+
45
+ text_input = gr.Textbox(label="Texto transcrito (o ingresa tu mensaje)", placeholder="El texto aparecerá aquí...")
46
+ text_output = gr.Textbox(label="Respuesta del Chatbot", placeholder="La respuesta del chatbot aparecerá aquí...", interactive=False)
47
+
48
+ b1 = gr.Button("Reconocer Audio y Preguntar al Chatbot")
49
+
50
+ def process_audio_and_chat(audio_input, history):
51
+ if audio_input:
52
+
53
+ audio_text = speech_to_text(audio_input)
54
+
55
+ response = chatbot(audio_text, history)
56
+ print(response)
57
+ return audio_text, response
58
+
59
+ return "", "Por favor, sube un archivo de audio."
60
+
61
+
62
+ b1.click(process_audio_and_chat, inputs=[audio_file, text_input], outputs=[text_input, text_output])
63
+
64
+
65
+ demo.launch(debug=True)