Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def encode_text(text):
|
13 |
-
inputs =
|
14 |
-
outputs =
|
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
|
|
|
19 |
user_embedding = encode_text(user_input)
|
20 |
-
response_embeddings = np.array([encode_text(resp) for resp in response_pool])
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
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 |
-
|
41 |
-
return
|
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="
|
49 |
-
description="A
|
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 |
+
|