musaashaikh commited on
Commit
c307bc4
·
verified ·
1 Parent(s): c45120e
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import torch
3
+ import gradio as gr
4
+
5
+
6
+ # Load a conversational model
7
+ model_name = "microsoft/DialoGPT-medium"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name)
10
+
11
+
12
+ def chatbot(input_text, history=[]):
13
+ # Tokenize input and history
14
+ inputs = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
15
+
16
+ # Append the conversation history
17
+ bot_input_ids = torch.cat([torch.tensor(history, dtype=torch.long), inputs], dim=-1) if history else inputs
18
+
19
+ # Generate response
20
+ history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
21
+ response = tokenizer.decode(history[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
22
+ return response, history.tolist()
23
+
24
+
25
+
26
+ # Define the interface
27
+ with gr.Blocks() as demo:
28
+ chatbot_widget = gr.Chatbot()
29
+ user_input = gr.Textbox(label="Type your message here")
30
+ submit_button = gr.Button("Send")
31
+
32
+ # Function to handle chat interactions
33
+ def respond(message, history=[]):
34
+ response, history = chatbot(message, history)
35
+ history.append((message, response)) # Append to chat history
36
+ return history, history
37
+
38
+ # Bind the button click to the chatbot response function
39
+ submit_button.click(respond, [user_input, chatbot_widget], [chatbot_widget, chatbot_widget])
40
+
41
+ # Launch the app
42
+ demo.launch()