miittnnss commited on
Commit
53f445e
Β·
1 Parent(s): af065f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -1,34 +1,33 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer
3
 
4
- # Create a conversational pipeline with DialoGPT-medium
5
- chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
 
6
 
7
- # Initialize the GPT-2 tokenizer
8
- gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2")
9
 
10
- def chat(message, history):
11
- # Use the GPT-2 tokenizer for user input
12
- user_input = gpt2_tokenizer.encode(message, return_tensors="pt")
13
-
14
- # Prepare conversation history as a list of dictionaries
15
- conversation = [{"role": "system", "content": "You are a helpful assistant."},
16
- {"role": "user", "content": message}]
17
 
18
- if history:
19
- conversation.append({"role": "assistant", "content": history})
20
-
21
  # Generate a response from DialoGPT-medium
22
- response = chatbot(conversation)
23
- bot_response = response[0]['message']['content']
24
-
 
 
 
 
25
  return bot_response
26
 
27
  # Create and launch the Gradio interface
28
- iface = gr.ChatInterface(
29
- chat,
30
  title="UrFriendly Chatbot",
31
- description="UrFriendly Chatbot is a conversational assistant based on DialoGPT-medium and GPT-2 tokenizer. Type or click on one of the examples to get started. Please note that UrFriendly Chatbot is not 100% accurate, so incorrect information may generate. πŸ’¬πŸ€—",
32
  examples=[
33
  "Howdy!",
34
  "Tell me a joke.",
@@ -37,10 +36,9 @@ iface = gr.ChatInterface(
37
  "What is an exponent in mathematics?",
38
  "Does money buy happiness?"
39
  ],
40
- retry_btn=None,
41
- undo_btn="Undo",
42
- clear_btn="Clear conversation"
43
  )
44
 
45
- iface.queue()
46
  iface.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
+ # Initialize the DialoGPT model and tokenizer
5
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
6
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
7
 
8
+ chat_history = None
 
9
 
10
+ def chat(message):
11
+ global chat_history
12
+
13
+ # Encode the user's message with the GPT-2 tokenizer
14
+ input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
 
 
15
 
 
 
 
16
  # Generate a response from DialoGPT-medium
17
+ response_ids = model.generate(input_ids, max_length=150, pad_token_id=tokenizer.eos_token_id, num_return_sequences=1)
18
+
19
+ # Decode and return the bot's response
20
+ bot_response = tokenizer.decode(response_ids[0], skip_special_tokens=True)
21
+
22
+ chat_history = bot_response # Store the bot's response for reference
23
+
24
  return bot_response
25
 
26
  # Create and launch the Gradio interface
27
+ iface = gr.Interface(
28
+ fn=chat,
29
  title="UrFriendly Chatbot",
30
+ description="UrFriendly Chatbot is a conversational assistant based on DialoGPT-medium with GPT-2 tokenization. Type or click on one of the examples to get started. Please note that UrFriendly Chatbot is not 100% accurate, so incorrect information may generate. πŸ’¬πŸ€—",
31
  examples=[
32
  "Howdy!",
33
  "Tell me a joke.",
 
36
  "What is an exponent in mathematics?",
37
  "Does money buy happiness?"
38
  ],
39
+ inputs="text",
40
+ outputs="text",
41
+ live=True # Set to True to allow continuous conversation
42
  )
43
 
 
44
  iface.launch()