s3nh commited on
Commit
d5fd417
1 Parent(s): 6228b9d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import torch
3
+ import gradio as gr
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("s3nh/DialoGPT-small-harry-potter-goblet-of-fire")
6
+ model = AutoModelForCausalLM.from_pretrained("s3nh/DialoGPT-small-harry-potter-goblet-of-fire")
7
+
8
+ def predict(input, history=[]):
9
+ # tokenize the new input sentence
10
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
11
+
12
+ # append the new user input tokens to the chat history
13
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
14
+
15
+ # generate a response
16
+ history = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id).tolist()
17
+
18
+ # convert the tokens to text, and then split the responses into lines
19
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
20
+ #print('decoded_response-->>'+str(response))
21
+ response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
22
+ #print('response-->>'+str(response))
23
+ return response, history
24
+
25
+ description = "Fine tuned dialogpt-small on Harry and The Goblet of Fire"
26
+ title = "Chat with Harry"
27
+ examples = [["Did you put your name in The Goblet of Fire?"]]
28
+ gr.Interface(fn=predict,
29
+ title=title,
30
+ description=description,
31
+ examples=examples,
32
+ inputs=["text", "state"],
33
+ outputs=["chatbot", "state"]).launch()