StSAdmin commited on
Commit
e5384df
·
verified ·
1 Parent(s): 7e4f7ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -10
app.py CHANGED
@@ -1,15 +1,42 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
2
 
3
  def chatbot(message, history):
4
- if "services" in message.lower():
5
- return "We offer managed IT, cybersecurity, and backup solutions. Want to schedule a consultation?"
6
- elif "hello" in message.lower():
7
- return "Hey there! 👋 SofTouch Systems here to help. How can I assist you today?"
8
- elif "support" in message.lower():
9
- return "You can email us at support@sofTouch.com or call our helpdesk at 123-456-7890."
10
- else:
11
- return "I’m still learning! Try asking about our services or how to contact us."
12
-
13
- iface = gr.ChatInterface(chatbot, type="messages")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  iface.launch(share=True)
15
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load pre-trained DialoGPT model and tokenizer
6
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
7
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
8
+
9
+ # Maintain conversation history
10
+ chat_history = []
11
 
12
  def chatbot(message, history):
13
+ global chat_history
14
+
15
+ # Combine user message with previous context
16
+ new_user_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
17
+
18
+ # Append new user input to chat history
19
+ bot_input_ids = torch.cat([i for i in chat_history] + [new_user_input_ids], dim=-1) if chat_history else new_user_input_ids
20
+
21
+ # Generate a response
22
+ chat_history_ids = model.generate(
23
+ bot_input_ids,
24
+ max_length=1000,
25
+ pad_token_id=tokenizer.eos_token_id,
26
+ do_sample=True,
27
+ top_k=50,
28
+ top_p=0.95
29
+ )
30
+
31
+ # Store response for future context
32
+ chat_history = [chat_history_ids]
33
+
34
+ # Decode response
35
+ response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
36
+
37
+ return response
38
+
39
+ iface = gr.ChatInterface(fn=chatbot, type="messages")
40
  iface.launch(share=True)
41
 
42
+