nugentc commited on
Commit
cdfe192
·
1 Parent(s): 0a042d8

try using the chatbot component again

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -1,28 +1,28 @@
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 the right format
18
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
19
- response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
20
- return response, history
21
 
22
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- gr.Interface(fn=predict,
25
- theme="default",
26
- css=".footer {display:none !important}",
27
- inputs=["text", "state"],
28
- outputs=["chatbot", "state"]).launch()
 
 
 
 
 
1
  from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import torch
3
+ import gradio as gr
4
+ import gradio as gr
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ def chat(message, history):
8
+ history = history or []
9
+ if message.startswith("How many"):
10
+ response = random.randint(1, 10)
11
+ elif message.startswith("How"):
12
+ response = random.choice(["Great", "Good", "Okay", "Bad"])
13
+ elif message.startswith("Where"):
14
+ response = random.choice(["Here", "There", "Somewhere"])
15
+ else:
16
+ response = "I don't know"
17
+ history.append((message, response))
18
+ return history, history
19
 
20
+ chatbot = gr.Chatbot(color_map=("green", "gray"))
21
+ demo = gr.Interface(
22
+ chat,
23
+ ["text", "state"],
24
+ [chatbot, "state"],
25
+ allow_screenshot=False,
26
+ allow_flagging="never",
27
+ )
28
+ demo.launch()