Ghmustafa11 commited on
Commit
897398a
·
verified ·
1 Parent(s): 324b355

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -1,32 +1,47 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- # Title of the application
5
- st.title("RozigGPT - AI Chatbot")
 
 
 
 
6
 
7
- # Introduction message
 
8
  st.markdown("""
9
  Welcome to RozigGPT! 🤖💬
10
  Chat with this AI assistant and get instant responses.
11
  """)
12
 
13
- # Initialize the Hugging Face pipeline for text generation (GPT-2 model)
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
- # Text input from the user
22
  user_input = st.text_input("You: ", "")
23
 
 
 
 
 
 
24
  if user_input:
25
- # Show user input
26
- st.write(f"You: {user_input}")
27
 
28
  # Get the bot's response
29
  bot_response = get_bot_response(user_input)
30
-
31
- # Show the bot's response
32
- st.write(f"RozigGPT: {bot_response}")
 
 
 
 
 
 
 
 
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)