gaja1995 commited on
Commit
834372e
·
verified ·
1 Parent(s): 629d6a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -2,40 +2,46 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import gradio as gr
3
  import torch
4
 
5
-
6
- title = "🤖AI ChatBot"
7
  description = "Building open-domain chatbots is a challenging area for machine learning research."
8
  examples = [["How are you?"]]
9
 
10
-
11
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
12
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
13
 
 
 
 
 
 
 
14
 
15
- def predict(input, history=[]):
16
- # tokenize the new input sentence
17
- new_user_input_ids = tokenizer.encode(
18
- input + tokenizer.eos_token, return_tensors="pt"
19
- )
 
20
 
21
- # append the new user input tokens to the chat history
22
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
 
23
 
24
- # generate a response
25
- history = model.generate(
26
- bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
27
- ).tolist()
28
 
29
- # convert the tokens to text, and then split the responses into lines
30
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
31
- # print('decoded_response-->>'+str(response))
32
- response = [
33
- (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
34
- ] # convert to tuples of list
35
- # print('response-->>'+str(response))
36
- return response, history
37
 
 
38
 
 
39
  gr.Interface(
40
  fn=predict,
41
  title=title,
@@ -43,5 +49,5 @@ gr.Interface(
43
  examples=examples,
44
  inputs=["text", "state"],
45
  outputs=["chatbot", "state"],
46
- theme="finlaymacklon/boxy_violet",
47
- ).launch()
 
2
  import gradio as gr
3
  import torch
4
 
5
+ # Title and description
6
+ title = "🤖 AI ChatBot"
7
  description = "Building open-domain chatbots is a challenging area for machine learning research."
8
  examples = [["How are you?"]]
9
 
10
+ # Load model and tokenizer
11
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
12
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
13
 
14
+ def predict(input_text, history=None):
15
+ if history is None:
16
+ history = []
17
+
18
+ # Tokenize new user input
19
+ new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
20
 
21
+ # Prepare chat history
22
+ if history:
23
+ past_ids = torch.LongTensor(history)
24
+ bot_input_ids = torch.cat([past_ids, new_user_input_ids], dim=-1)
25
+ else:
26
+ bot_input_ids = new_user_input_ids
27
 
28
+ # Generate response
29
+ output_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
30
+ history = output_ids.tolist()
31
 
32
+ # Decode and extract bot reply
33
+ decoded_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
34
+ user_reply = input_text
35
+ bot_reply = decoded_text.split(input_text)[-1].strip()
36
 
37
+ # Format chatbot UI output
38
+ chatbot_messages = []
39
+ if len(history) > 0:
40
+ chatbot_messages = [(user_reply, bot_reply)]
 
 
 
 
41
 
42
+ return chatbot_messages, history
43
 
44
+ # Gradio interface
45
  gr.Interface(
46
  fn=predict,
47
  title=title,
 
49
  examples=examples,
50
  inputs=["text", "state"],
51
  outputs=["chatbot", "state"],
52
+ theme="finlaymacklon/boxy_violet"
53
+ ).launch()