Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Set up page title and icon
|
| 5 |
+
st.set_page_config(page_title="π Roleplay Chatbot", page_icon="π€")
|
| 6 |
+
|
| 7 |
+
# Load pre-trained chatbot model from Hugging Face
|
| 8 |
+
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 9 |
+
|
| 10 |
+
st.title("π Roleplay AI Chatbot")
|
| 11 |
+
st.write("Chat with an AI character!")
|
| 12 |
+
|
| 13 |
+
# Initialize chat history
|
| 14 |
+
if "messages" not in st.session_state:
|
| 15 |
+
st.session_state["messages"] = []
|
| 16 |
+
|
| 17 |
+
# Display chat history
|
| 18 |
+
for message in st.session_state["messages"]:
|
| 19 |
+
avatar = "π§ββοΈ" if message["role"] == "assistant" else "π§βπ»"
|
| 20 |
+
with st.chat_message(message["role"], avatar=avatar):
|
| 21 |
+
st.markdown(message["content"])
|
| 22 |
+
|
| 23 |
+
# User input field
|
| 24 |
+
user_input = st.chat_input("Type your message...")
|
| 25 |
+
|
| 26 |
+
if user_input:
|
| 27 |
+
# Add user input to chat history
|
| 28 |
+
st.session_state["messages"].append({"role": "user", "content": user_input})
|
| 29 |
+
|
| 30 |
+
# Show typing animation
|
| 31 |
+
with st.chat_message("assistant", avatar="π§ββοΈ"):
|
| 32 |
+
with st.spinner("Typing..."):
|
| 33 |
+
response = chatbot(user_input, max_length=100, do_sample=True, temperature=0.7)
|
| 34 |
+
bot_reply = response[0]["generated_text"]
|
| 35 |
+
st.markdown(bot_reply)
|
| 36 |
+
|
| 37 |
+
# Save bot response to chat history
|
| 38 |
+
st.session_state["messages"].append({"role": "assistant", "content": bot_reply})
|