Spaces:
Runtime error
Runtime error
Upload app.py.py
Browse files
app.py.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Meena_A_Multilingual_Chatbot (1).ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1-IfUcnDUppyMArHonc_iesEcN2gSKU-j
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip3 install transformers
|
11 |
+
!pip install -q translate
|
12 |
+
!pip install polyglot
|
13 |
+
|
14 |
+
!pip install Pyicu
|
15 |
+
|
16 |
+
!pip install Morfessor
|
17 |
+
!pip install pycld2
|
18 |
+
|
19 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
20 |
+
import torch
|
21 |
+
from translate import Translator
|
22 |
+
from polyglot.detect import Detector
|
23 |
+
|
24 |
+
# model_name = "microsoft/DialoGPT-large"
|
25 |
+
model_name = "microsoft/DialoGPT-large"
|
26 |
+
# model_name = "microsoft/DialoGPT-small"
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
29 |
+
|
30 |
+
# # chatting 5 times with nucleus sampling & tweaking temperature
|
31 |
+
# step=-1
|
32 |
+
# while(True):
|
33 |
+
# step+=1
|
34 |
+
# # take user input
|
35 |
+
# text = input(">> You:>")
|
36 |
+
# detected_language=Detector(text,quiet=True).language.code
|
37 |
+
# translator=Translator(from_lang=detected_language,to_lang="en")
|
38 |
+
# translated_input=translator.translate(text)
|
39 |
+
# print(translated_input)
|
40 |
+
# if text.lower().find("bye")!=-1:
|
41 |
+
# print(f">> Meena:> Bye Bye!")
|
42 |
+
# break;
|
43 |
+
# # encode the input and add end of string token
|
44 |
+
# input_ids = tokenizer.encode(translated_input+tokenizer.eos_token, return_tensors="pt")
|
45 |
+
# # concatenate new user input with chat history (if there is)
|
46 |
+
# bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
|
47 |
+
# # generate a bot response
|
48 |
+
# chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id,do_sample=True,top_p=0.9,top_k=50,temperature=0.7,num_beams=5,no_repeat_ngram_size=2)
|
49 |
+
# #print the output
|
50 |
+
# output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
51 |
+
# print(output)
|
52 |
+
# translator=Translator(from_lang="en",to_lang=detected_language)
|
53 |
+
# translated_output=translator.translate(output)
|
54 |
+
|
55 |
+
# print(f">> Meena:> {translated_output}")
|
56 |
+
|
57 |
+
!pip install gradio
|
58 |
+
|
59 |
+
import gradio as gr
|
60 |
+
|
61 |
+
def generate_text(text):
|
62 |
+
step=-1
|
63 |
+
while(True):
|
64 |
+
step+=1
|
65 |
+
detected_language=Detector(text,quiet=True).language.code
|
66 |
+
translator=Translator(from_lang=detected_language,to_lang="en")
|
67 |
+
translated_input=translator.translate(text)
|
68 |
+
|
69 |
+
if text.lower().find("bye")!=-1:
|
70 |
+
print(f">> Meena:> Bye Bye!")
|
71 |
+
break;
|
72 |
+
# encode the input and add end of string token
|
73 |
+
input_ids = tokenizer.encode(translated_input+tokenizer.eos_token, return_tensors="pt")
|
74 |
+
# concatenate new user input with chat history (if there is)
|
75 |
+
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
|
76 |
+
# generate a bot response
|
77 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id,do_sample=True,top_p=0.9,top_k=50,temperature=0.7,num_beams=5,no_repeat_ngram_size=2)
|
78 |
+
#print the output
|
79 |
+
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
80 |
+
|
81 |
+
translator=Translator(from_lang="en",to_lang=detected_language)
|
82 |
+
translated_output=translator.translate(output)
|
83 |
+
|
84 |
+
return translated_output
|
85 |
+
|
86 |
+
output_text=gr.Textbox()
|
87 |
+
gr.Interface(generate_text,"textbox",output_text,title="Meena",
|
88 |
+
description="Meena- Amultilingual Chatbot").launch(debug=False,share=True)
|
89 |
+
|
90 |
+
!gradio deploy
|