Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Load the pre-trained model and tokenizer
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
model_name = "microsoft/DialoGPT-medium" # Replace with your preferred model
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
return model, tokenizer
|
11 |
+
|
12 |
+
model, tokenizer = load_model()
|
13 |
+
|
14 |
+
# Chat history
|
15 |
+
if "messages" not in st.session_state:
|
16 |
+
st.session_state["messages"] = []
|
17 |
+
|
18 |
+
# Sidebar configuration
|
19 |
+
st.sidebar.title("Chatbot Settings")
|
20 |
+
st.sidebar.write("Customize your chatbot:")
|
21 |
+
max_length = st.sidebar.slider("Max Response Length", 10, 200, 50)
|
22 |
+
temperature = st.sidebar.slider("Response Creativity (Temperature)", 0.1, 1.0, 0.7)
|
23 |
+
|
24 |
+
# App title
|
25 |
+
st.title("🤖 Open Source Text-to-Text Chatbot")
|
26 |
+
|
27 |
+
# Chat Interface
|
28 |
+
st.write("### Chat with the bot:")
|
29 |
+
user_input = st.text_input("You:", key="user_input", placeholder="Type your message here...")
|
30 |
+
|
31 |
+
if user_input:
|
32 |
+
# Encode the input and add chat history for context
|
33 |
+
inputs = tokenizer.encode(
|
34 |
+
" ".join(st.session_state["messages"] + [user_input]),
|
35 |
+
return_tensors="pt",
|
36 |
+
truncation=True
|
37 |
+
)
|
38 |
+
|
39 |
+
# Generate response
|
40 |
+
response = model.generate(
|
41 |
+
inputs,
|
42 |
+
max_length=max_length,
|
43 |
+
temperature=temperature,
|
44 |
+
pad_token_id=tokenizer.eos_token_id,
|
45 |
+
)
|
46 |
+
bot_response = tokenizer.decode(response[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
47 |
+
|
48 |
+
# Append to chat history
|
49 |
+
st.session_state["messages"].append(f"You: {user_input}")
|
50 |
+
st.session_state["messages"].append(f"Bot: {bot_response}")
|
51 |
+
|
52 |
+
# Display the chat
|
53 |
+
for message in st.session_state["messages"]:
|
54 |
+
if message.startswith("You:"):
|
55 |
+
st.markdown(f"**{message}**")
|
56 |
+
else:
|
57 |
+
st.markdown(f"> {message}")
|
58 |
+
|
59 |
+
# Clear chat history button
|
60 |
+
if st.button("Clear Chat"):
|
61 |
+
st.session_state["messages"] = []
|
62 |
+
st.experimental_rerun()
|