tstone87 commited on
Commit
d55c517
·
verified ·
1 Parent(s): fdba148

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+
4
+ # Load the model and tokenizer
5
+ model_name = "microsoft/DialoGPT-small"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+ # Function to generate a response
10
+ def dialoGPT_response(user_input, history):
11
+ # Encode the new user input, with the history
12
+ new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
13
+
14
+ # Append the new user input tokens to the chat history
15
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1) if history else new_user_input_ids
16
+
17
+ # Generate a response
18
+ chat_history_ids = model.generate(
19
+ bot_input_ids,
20
+ max_length=1000,
21
+ pad_token_id=tokenizer.eos_token_id
22
+ )
23
+
24
+ # Decode the response
25
+ response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
26
+ return response
27
+
28
+ # Gradio interface
29
+ iface = gr.Interface(
30
+ fn=dialoGPT_response,
31
+ inputs=[gr.Textbox(placeholder="Enter your message..."), "state"],
32
+ outputs="text",
33
+ title="DialoGPT Chat",
34
+ description="Chat with DialoGPT-small model."
35
+ )
36
+
37
+ iface.launch()