Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import streamlit as st
|
|
| 2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
model = GPT2LMHeadModel.from_pretrained("microsoft/DialoGPT-medium").to(device)
|
|
@@ -14,45 +14,49 @@ def load_model():
|
|
| 14 |
|
| 15 |
model, tokenizer = load_model()
|
| 16 |
|
| 17 |
-
# Function to generate
|
| 18 |
-
def get_response(
|
| 19 |
-
|
| 20 |
-
inputs = tokenizer.encode(input_text
|
| 21 |
-
|
| 22 |
-
# Generate the response
|
| 23 |
with torch.no_grad():
|
| 24 |
-
outputs = model.generate(
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Streamlit app interface
|
| 32 |
-
st.title("
|
| 33 |
-
|
| 34 |
-
# Display instructions
|
| 35 |
-
st.write("### Chat with the DialoGPT model. Type your message below and press Enter!")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
if "messages" not in st.session_state:
|
| 39 |
st.session_state.messages = []
|
| 40 |
|
| 41 |
-
# Display
|
| 42 |
for message in st.session_state.messages:
|
| 43 |
-
st.markdown(f"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
user_input = st.text_input("You: ", "")
|
| 47 |
|
| 48 |
-
#
|
| 49 |
if user_input:
|
| 50 |
-
# Add user
|
| 51 |
st.session_state.messages.append({"role": "User", "content": user_input})
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
st.session_state.messages.append({"role": "Chatbot", "content": response})
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
st.text_input("You: ", "",
|
|
|
|
| 2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Configure device (CPU/GPU)
|
| 6 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
|
| 8 |
+
# Load the model and tokenizer
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
model = GPT2LMHeadModel.from_pretrained("microsoft/DialoGPT-medium").to(device)
|
|
|
|
| 14 |
|
| 15 |
model, tokenizer = load_model()
|
| 16 |
|
| 17 |
+
# Function to generate chatbot response
|
| 18 |
+
def get_response(conversation_history, user_input):
|
| 19 |
+
input_text = " ".join(conversation_history) + " " + user_input + tokenizer.eos_token
|
| 20 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 21 |
+
|
|
|
|
| 22 |
with torch.no_grad():
|
| 23 |
+
outputs = model.generate(
|
| 24 |
+
inputs,
|
| 25 |
+
max_length=75,
|
| 26 |
+
num_return_sequences=1,
|
| 27 |
+
no_repeat_ngram_size=2,
|
| 28 |
+
top_k=40,
|
| 29 |
+
top_p=0.8,
|
| 30 |
+
temperature=0.7
|
| 31 |
+
)
|
| 32 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
|
| 34 |
# Streamlit app interface
|
| 35 |
+
st.title("Chatbot with Hugging Face Model")
|
| 36 |
+
st.write("### Chat with the chatbot powered by DialoGPT. Type your message below!")
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Initialize conversation history
|
| 39 |
if "messages" not in st.session_state:
|
| 40 |
st.session_state.messages = []
|
| 41 |
|
| 42 |
+
# Display chat history
|
| 43 |
for message in st.session_state.messages:
|
| 44 |
+
st.markdown(f"{message['role']}: {message['content']}")
|
| 45 |
|
| 46 |
+
# User input
|
| 47 |
+
user_input = st.text_input("You: ", key="user_input")
|
| 48 |
|
| 49 |
+
# Generate response if user submits a message
|
| 50 |
if user_input:
|
| 51 |
+
# Add user input to conversation history
|
| 52 |
st.session_state.messages.append({"role": "User", "content": user_input})
|
| 53 |
+
|
| 54 |
+
# Prepare context for the chatbot
|
| 55 |
+
history = [msg["content"] for msg in st.session_state.messages[-3:] if msg["role"] == "User"]
|
| 56 |
+
|
| 57 |
+
# Generate chatbot response
|
| 58 |
+
response = get_response(history, user_input)
|
| 59 |
st.session_state.messages.append({"role": "Chatbot", "content": response})
|
| 60 |
|
| 61 |
+
# Clear input box for new input
|
| 62 |
+
st.text_input("You: ", key="user_input", value="")
|