Spaces:
Sleeping
Sleeping
Commit
·
e85cffd
1
Parent(s):
5389890
Update app.py
Browse files
app.py
CHANGED
@@ -143,9 +143,9 @@ def text2text_summary(para):
|
|
143 |
# grad.Interface(text2text_summary, inputs=para, outputs=out).launch()
|
144 |
|
145 |
# T5 Translate
|
146 |
-
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
147 |
-
import gradio as grad
|
148 |
-
text2text_tkn= T5Tokenizer.from_pretrained("t5-small")
|
149 |
mdl = T5ForConditionalGeneration.from_pretrained("t5-small")
|
150 |
def text2text_translation(text):
|
151 |
inp = "translate English to Portuguese: "+text
|
@@ -153,6 +153,58 @@ def text2text_translation(text):
|
|
153 |
tokens = mdl.generate(**enc, max_length=100, num_return_sequences=1, early_stopping=True)
|
154 |
response=text2text_tkn.decode(tokens[0], skip_special_tokens=True)
|
155 |
return response
|
156 |
-
para=grad.Textbox(lines=1, label="English Text", placeholder="Text in English")
|
157 |
-
out=grad.Textbox(lines=1, label="Portuguese Translation")
|
158 |
-
grad.Interface(text2text_translation, inputs=para, outputs=out).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
# grad.Interface(text2text_summary, inputs=para, outputs=out).launch()
|
144 |
|
145 |
# T5 Translate
|
146 |
+
# from transformers import T5ForConditionalGeneration, T5Tokenizer
|
147 |
+
# import gradio as grad
|
148 |
+
# text2text_tkn= T5Tokenizer.from_pretrained("t5-small")
|
149 |
mdl = T5ForConditionalGeneration.from_pretrained("t5-small")
|
150 |
def text2text_translation(text):
|
151 |
inp = "translate English to Portuguese: "+text
|
|
|
153 |
tokens = mdl.generate(**enc, max_length=100, num_return_sequences=1, early_stopping=True)
|
154 |
response=text2text_tkn.decode(tokens[0], skip_special_tokens=True)
|
155 |
return response
|
156 |
+
# para=grad.Textbox(lines=1, label="English Text", placeholder="Text in English")
|
157 |
+
# out=grad.Textbox(lines=1, label="Portuguese Translation")
|
158 |
+
# grad.Interface(text2text_translation, inputs=para, outputs=out).launch()
|
159 |
+
|
160 |
+
|
161 |
+
|
162 |
+
# ChatBot
|
163 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer,BlenderbotForConditionalGeneration
|
164 |
+
import torch
|
165 |
+
chat_tkn = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
166 |
+
mdl = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
167 |
+
#chat_tkn = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
168 |
+
#mdl = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
|
169 |
+
|
170 |
+
|
171 |
+
def converse(user_input, chat_history=[]):
|
172 |
+
user_input_ids = chat_tkn(user_input + chat_tkn.eos_token, return_tensors='pt').input_ids
|
173 |
+
# keep history in the tensor
|
174 |
+
bot_input_ids = torch.cat([torch.LongTensor(chat_history), user_input_ids], dim=-1)
|
175 |
+
# get response
|
176 |
+
chat_history = mdl.generate(bot_input_ids, max_length=1000, pad_token_id=chat_tkn.eos_token_id).tolist()
|
177 |
+
print (chat_history)
|
178 |
+
response = chat_tkn.decode(chat_history[0]).split("<|endoftext|>")
|
179 |
+
print("starting to print response")
|
180 |
+
print(response)
|
181 |
+
# html for display
|
182 |
+
html = "<div class='mybot'>"
|
183 |
+
for x, mesg in enumerate(response):
|
184 |
+
if x%2!=0 :
|
185 |
+
mesg="Alicia:"+mesg
|
186 |
+
clazz="alicia"
|
187 |
+
else :
|
188 |
+
clazz="user"
|
189 |
+
print("value of x")
|
190 |
+
print(x)
|
191 |
+
print("message")
|
192 |
+
print (mesg)
|
193 |
+
html += "<div class='mesg {}'> {}</div>".format(clazz, mesg)
|
194 |
+
html += "</div>"
|
195 |
+
print(html)
|
196 |
+
return html, chat_history
|
197 |
+
import gradio as grad
|
198 |
+
css = """
|
199 |
+
.mychat {display:flex;flex-direction:column}
|
200 |
+
.mesg {padding:5px;margin-bottom:5px;border-radius:5px;width:75%}
|
201 |
+
.mesg.user {background-color:lightblue;color:white}
|
202 |
+
.mesg.alicia {background-color:orange;color:white,align-self:self-end}
|
203 |
+
.footer {display:none !important}
|
204 |
+
"""
|
205 |
+
text=grad.inputs.Textbox(placeholder="Lets chat")
|
206 |
+
grad.Interface(fn=converse,
|
207 |
+
theme="default",
|
208 |
+
inputs=[text, "state"],
|
209 |
+
outputs=["html", "state"],
|
210 |
+
css=css).launch()
|