eradhea commited on
Commit
f8841d9
·
1 Parent(s): fb676c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration, pipeline
2
+ import gradio as gr
3
+
4
+ mname = "facebook/blenderbot-400M-distill"
5
+
6
+ model = BlenderbotForConditionalGeneration.from_pretrained(mname)
7
+
8
+ tokenizer = BlenderbotTokenizer.from_pretrained(mname)
9
+
10
+ to_english = pipeline('translation', model='Helsinki-NLP/opus-mt-es-en')
11
+
12
+ to_spanish = pipeline('translation', model='Helsinki-NLP/opus-mt-en-es')
13
+
14
+ def chat(msg, history):
15
+
16
+ history = history or []
17
+ mensaje = msg
18
+ MESSAGE = to_english(mensaje)
19
+ inputs = tokenizer([MESSAGE[0]['translation_text']], return_tensors="pt")
20
+ reply_ids = model.generate(**inputs)
21
+ respuesta = to_spanish(tokenizer.batch_decode(reply_ids))
22
+ history.append((msg,respuesta[0]['translation_text']))
23
+
24
+ return history, history
25
+
26
+ chatbot = gr.Chatbot().style(color_map=("red", "blue"))
27
+ demo = gr.Interface(
28
+ chat,
29
+ ["text", "state"],
30
+ [chatbot, "state"],
31
+ allow_flagging="never",
32
+ )
33
+
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch()