File size: 1,244 Bytes
7726f56
 
 
 
 
 
 
eb6bdab
7726f56
 
eb6bdab
7726f56
eb6bdab
7726f56
 
 
 
 
 
 
 
 
 
 
 
 
1cbafc5
 
 
 
 
7726f56
1cbafc5
7726f56
2c059ee
7726f56
 
2c059ee
a299930
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration, pipeline
import gradio as gr
from textblob import TextBlob as tb

mname = "facebook/blenderbot-400M-distill"
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
tokenizer = BlenderbotTokenizer.from_pretrained(mname)
#p = pipeline("automatic-speech-recognition", model = "facebook/wav2vec2-large-xlsr-53-spanish")


def transcribe(texto): # Desde un mensaje en español

    mensaje_voz = texto
    blob = tb(mensaje_voz)
    MESSAGE = str(blob.translate(from_lang='es', to='en')) # Traduce el mensaje al inglés

    inputs = tokenizer(MESSAGE, return_tensors="pt") # Tokeniza el mensaje traducido
    reply_ids = model.generate(**inputs)

    response = tokenizer.batch_decode(reply_ids)[0] # Genera la respuesta (en inglés)

    blob_2 = tb(response)
    respuesta = str(blob_2.translate(from_lang='en', to='es'))[4:-5] # Traduce la respuesta al español
    
    return respuesta

def chatbot(input, history=[]):
    output = transcribe(input)
    history.append((input, output))
    return history, history
    
gr.Interface(
    fn=chatbot, 
    inputs=[
        "text",'state'
    ],
    outputs=[
        "chatbot",'state',
    ]
).launch()