abidlabs HF staff commited on
Commit
3d414e0
1 Parent(s): 8b9373a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+
4
+ HF_TOKEN = os.getenv('HF_TOKEN')
5
+ hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "Rick-bot-flags")
6
+
7
+ title = "Talk To Me Morty"
8
+ description = """
9
+ <p>
10
+ <center>
11
+ The bot was trained on Rick and Morty dialogues Kaggle Dataset using DialoGPT.
12
+ <img src="https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot/resolve/main/img/rick.png" alt="rick" width="200"/>
13
+ </center>
14
+ </p>
15
+ """
16
+ article = "<p style='text-align: center'><a href='https://medium.com/geekculture/discord-bot-using-dailogpt-and-huggingface-api-c71983422701' target='_blank'>Complete Tutorial</a></p><p style='text-align: center'><a href='https://dagshub.com/kingabzpro/DailoGPT-RickBot' target='_blank'>Project is Available at DAGsHub</a></p></center></p>"
17
+
18
+ from transformers import AutoModelForCausalLM, AutoTokenizer
19
+ import torch
20
+
21
+ tokenizer = AutoTokenizer.from_pretrained("ericzhou/DialoGPT-Medium-Rick_v2")
22
+ model = AutoModelForCausalLM.from_pretrained("ericzhou/DialoGPT-Medium-Rick_v2")
23
+
24
+ def predict(input, history=[]):
25
+ # tokenize the new input sentence
26
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
27
+
28
+ # append the new user input tokens to the chat history
29
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
30
+
31
+ # generate a response
32
+ history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
33
+
34
+ # convert the tokens to text, and then split the responses into the right format
35
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
36
+ response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
37
+ return response, history
38
+
39
+ gr.Interface(fn = predict, inputs = ["textbox","state"], outputs = ["chatbot","state"],allow_flagging = "manual",title = title, flagging_callback = hf_writer, description = description, article = article ).launch(enable_queue=True) # customizes the input component
40
+
41
+ #theme ="grass",
42
+ #title = title,
43
+ #flagging_callback=hf_writer,
44
+ #description = description,
45
+ #article = article