Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 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 = "
|
| 9 |
bi_tokenizer = AutoTokenizer.from_pretrained(bi_encoder_model_name)
|
| 10 |
bi_model = AutoModel.from_pretrained(bi_encoder_model_name)
|
| 11 |
|
|
@@ -20,28 +18,36 @@ def encode_text(text):
|
|
| 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 |
-
#
|
| 25 |
-
|
| 26 |
|
| 27 |
-
# Generate a response using GPT-2
|
| 28 |
-
gpt2_inputs = gpt2_tokenizer.encode(
|
| 29 |
-
gpt2_outputs = gpt2_model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 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
|
|
@@ -49,3 +55,4 @@ iface.launch()
|
|
| 49 |
|
| 50 |
|
| 51 |
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModel, GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load the bi-encoder model and tokenizer
|
| 6 |
+
bi_encoder_model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
| 7 |
bi_tokenizer = AutoTokenizer.from_pretrained(bi_encoder_model_name)
|
| 8 |
bi_model = AutoModel.from_pretrained(bi_encoder_model_name)
|
| 9 |
|
|
|
|
| 18 |
# Ensure the output is 2D by averaging the last hidden state along the sequence dimension
|
| 19 |
return outputs.last_hidden_state.mean(dim=1).detach().numpy()
|
| 20 |
|
| 21 |
+
def generate_response(user_input, context_embedding):
|
| 22 |
+
# Combine user input with context embedding for GPT-2 input
|
| 23 |
+
combined_input = user_input + " " + context_embedding
|
| 24 |
|
| 25 |
+
# Generate a response using GPT-2 with adjusted parameters
|
| 26 |
+
gpt2_inputs = gpt2_tokenizer.encode(combined_input, return_tensors='pt')
|
| 27 |
+
gpt2_outputs = gpt2_model.generate(
|
| 28 |
+
gpt2_inputs,
|
| 29 |
+
max_length=150,
|
| 30 |
+
num_return_sequences=1,
|
| 31 |
+
temperature=0.5,
|
| 32 |
+
top_p=0.9,
|
| 33 |
+
repetition_penalty=1.2
|
| 34 |
+
)
|
| 35 |
generated_text = gpt2_tokenizer.decode(gpt2_outputs[0], skip_special_tokens=True)
|
| 36 |
|
| 37 |
return generated_text
|
| 38 |
|
| 39 |
+
def chatbot(user_input, context=""):
|
| 40 |
+
context_embedding = encode_text(context) if context else ""
|
| 41 |
+
response = generate_response(user_input, context_embedding)
|
| 42 |
return response
|
| 43 |
|
| 44 |
# Create the Gradio interface
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=chatbot,
|
| 47 |
+
inputs=[gr.Textbox(lines=2, placeholder="Enter your message here..."), gr.Textbox(lines=2, placeholder="Enter context here (optional)...")],
|
| 48 |
outputs="text",
|
| 49 |
+
title="Context-Aware Dynamic Response Chatbot",
|
| 50 |
+
description="A chatbot using a bi-encoder model to understand the input context and GPT-2 to generate dynamic responses."
|
| 51 |
)
|
| 52 |
|
| 53 |
# Launch the interface
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
+
|