Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
+
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
5 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
+
|
7 |
+
def predict(input, history=[]):
|
8 |
+
# tokenize the new input sentence
|
9 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
10 |
+
|
11 |
+
# append the new user input tokens to the chat history
|
12 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
13 |
+
|
14 |
+
# generate a response
|
15 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
16 |
+
|
17 |
+
# convert the tokens to text, and then split the responses into lines
|
18 |
+
response = tokenizer.decode(history[0]).replace("<|endoftext|>", "\n")
|
19 |
+
|
20 |
+
return response, history
|
21 |
+
|
22 |
+
import gradio as gr
|
23 |
+
|
24 |
+
gr.Interface(fn=predict,
|
25 |
+
inputs=["text", "state"],
|
26 |
+
outputs=["text", "state"]).launch()
|
27 |
+
|