Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,47 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
|
|
|
| 8 |
st.markdown("""
|
| 9 |
Welcome to RozigGPT! 🤖💬
|
| 10 |
Chat with this AI assistant and get instant responses.
|
| 11 |
""")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
generator = pipeline("text-generation", model="gpt2") # Correct model identifier: "gpt2"
|
| 15 |
-
|
| 16 |
-
# Function to get the bot's response
|
| 17 |
def get_bot_response(user_input):
|
|
|
|
| 18 |
response = generator(user_input, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 19 |
return response[0]['generated_text']
|
| 20 |
|
| 21 |
-
#
|
| 22 |
user_input = st.text_input("You: ", "")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if user_input:
|
| 25 |
-
#
|
| 26 |
-
st.
|
| 27 |
|
| 28 |
# Get the bot's response
|
| 29 |
bot_response = get_bot_response(user_input)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Initialize the GPT-2 model for text generation using Hugging Face pipeline
|
| 5 |
+
generator = pipeline("text-generation", model="gpt2")
|
| 6 |
+
|
| 7 |
+
# Initialize session state to keep track of conversation history
|
| 8 |
+
if "messages" not in st.session_state:
|
| 9 |
+
st.session_state.messages = []
|
| 10 |
|
| 11 |
+
# Title and introduction of the app
|
| 12 |
+
st.title("RozigGPT - AI Chatbot")
|
| 13 |
st.markdown("""
|
| 14 |
Welcome to RozigGPT! 🤖💬
|
| 15 |
Chat with this AI assistant and get instant responses.
|
| 16 |
""")
|
| 17 |
|
| 18 |
+
# Function to generate the bot's response
|
|
|
|
|
|
|
|
|
|
| 19 |
def get_bot_response(user_input):
|
| 20 |
+
# Generate text using GPT-2 model (you can increase max_length for more detailed answers)
|
| 21 |
response = generator(user_input, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 22 |
return response[0]['generated_text']
|
| 23 |
|
| 24 |
+
# Input field for the user to type their message
|
| 25 |
user_input = st.text_input("You: ", "")
|
| 26 |
|
| 27 |
+
# Button to clear chat history
|
| 28 |
+
if st.button("Clear Chat"):
|
| 29 |
+
st.session_state.messages = []
|
| 30 |
+
|
| 31 |
+
# Process user input
|
| 32 |
if user_input:
|
| 33 |
+
# Add user message to session state
|
| 34 |
+
st.session_state.messages.append(f"You: {user_input}")
|
| 35 |
|
| 36 |
# Get the bot's response
|
| 37 |
bot_response = get_bot_response(user_input)
|
| 38 |
+
st.session_state.messages.append(f"RozigGPT: {bot_response}")
|
| 39 |
+
|
| 40 |
+
# Display conversation history
|
| 41 |
+
for message in st.session_state.messages:
|
| 42 |
+
if message.startswith("You:"):
|
| 43 |
+
# Display user messages on the left side
|
| 44 |
+
st.markdown(f"<p style='text-align: left; color: blue;'>{message}</p>", unsafe_allow_html=True)
|
| 45 |
+
else:
|
| 46 |
+
# Display bot messages on the right side
|
| 47 |
+
st.markdown(f"<p style='text-align: right; color: green;'>{message}</p>", unsafe_allow_html=True)
|