aquibmoin commited on
Commit
11d423c
1 Parent(s): 76e9582

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -1,55 +1,51 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModel
3
  import torch
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import numpy as np
6
 
7
- # Load the model and tokenizer
8
- model_name = "nasa-impact/nasa-smd-ibm-st-v2"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModel.from_pretrained(model_name)
 
 
 
 
 
11
 
12
  def encode_text(text):
13
- inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=128)
14
- outputs = model(**inputs)
15
  # Ensure the output is 2D by averaging the last hidden state along the sequence dimension
16
  return outputs.last_hidden_state.mean(dim=1).detach().numpy()
17
 
18
- def find_best_response(user_input, response_pool):
 
19
  user_embedding = encode_text(user_input)
20
- response_embeddings = np.array([encode_text(resp) for resp in response_pool])
21
 
22
- # Ensure response_embeddings are 2D
23
- if response_embeddings.ndim == 3:
24
- response_embeddings = response_embeddings.squeeze(axis=1)
 
25
 
26
- similarities = cosine_similarity(user_embedding, response_embeddings).flatten()
27
- best_response_index = np.argmax(similarities)
28
- return response_pool[best_response_index]
29
-
30
- # Define some example responses for the chatbot to choose from
31
- response_pool = [
32
- "Hello! How can I help you today?",
33
- "I'm here to assist you with any questions you have.",
34
- "What would you like to know more about?",
35
- "Can you please provide more details?",
36
- "I'm not sure about that. Could you clarify?"
37
- ]
38
 
39
  def chatbot(user_input):
40
- best_response = find_best_response(user_input, response_pool)
41
- return best_response
42
 
43
  # Create the Gradio interface
44
  iface = gr.Interface(
45
  fn=chatbot,
46
  inputs=gr.Textbox(lines=2, placeholder="Enter your message here..."),
47
  outputs="text",
48
- title="Bi-encoder Chatbot",
49
- description="A simple chatbot using a bi-encoder model to find the best response."
50
  )
51
 
52
  # Launch the interface
53
  iface.launch()
54
 
55
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModel, GPT2LMHeadModel, GPT2Tokenizer
3
  import torch
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import numpy as np
6
 
7
+ # Load the bi-encoder model and tokenizer
8
+ bi_encoder_model_name = "nasa-impact/nasa-smd-ibm-st-v2"
9
+ bi_tokenizer = AutoTokenizer.from_pretrained(bi_encoder_model_name)
10
+ bi_model = AutoModel.from_pretrained(bi_encoder_model_name)
11
+
12
+ # Load the GPT-2 model and tokenizer for response generation
13
+ gpt2_model_name = "gpt2"
14
+ gpt2_tokenizer = GPT2Tokenizer.from_pretrained(gpt2_model_name)
15
+ gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_model_name)
16
 
17
  def encode_text(text):
18
+ inputs = bi_tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=128)
19
+ outputs = bi_model(**inputs)
20
  # Ensure the output is 2D by averaging the last hidden state along the sequence dimension
21
  return outputs.last_hidden_state.mean(dim=1).detach().numpy()
22
 
23
+ def generate_response(user_input):
24
+ # Encode the user input
25
  user_embedding = encode_text(user_input)
 
26
 
27
+ # Generate a response using GPT-2
28
+ gpt2_inputs = gpt2_tokenizer.encode(user_input, return_tensors='pt')
29
+ gpt2_outputs = gpt2_model.generate(gpt2_inputs, max_length=150, num_return_sequences=1)
30
+ generated_text = gpt2_tokenizer.decode(gpt2_outputs[0], skip_special_tokens=True)
31
 
32
+ return generated_text
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def chatbot(user_input):
35
+ response = generate_response(user_input)
36
+ return response
37
 
38
  # Create the Gradio interface
39
  iface = gr.Interface(
40
  fn=chatbot,
41
  inputs=gr.Textbox(lines=2, placeholder="Enter your message here..."),
42
  outputs="text",
43
+ title="Dynamic Response Chatbot",
44
+ description="A chatbot using a bi-encoder model to understand the input and GPT-2 to generate dynamic responses."
45
  )
46
 
47
  # Launch the interface
48
  iface.launch()
49
 
50
 
51
+