Eldeeb commited on
Commit
b135402
·
verified ·
1 Parent(s): 5250f7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -13
app.py CHANGED
@@ -2,40 +2,68 @@
2
  import streamlit as st
3
  from transformers import pipeline
4
 
5
- # Initialize the model
6
- pipe = pipeline("text2text-generation", model="facebook/blenderbot-400M-distill")
 
 
7
 
8
- # Initialize session state for conversation history
 
 
 
9
  if 'conversation_history' not in st.session_state:
10
  st.session_state.conversation_history = ""
 
 
11
 
12
  def converse(user_message):
13
  # Update the conversation history
14
  st.session_state.conversation_history += f"User: {user_message}\n"
15
  result = pipe(st.session_state.conversation_history)[0]['generated_text']
16
  st.session_state.conversation_history += f"Bot: {result}\n"
 
17
  return result
18
 
19
- # Streamlit app
20
- st.title("AI Chatbot")
21
- st.write("Chat with the AI bot!")
 
 
 
 
 
 
 
22
 
23
- # Text input from the user
24
- user_message = st.text_input("Enter Your message:")
 
25
 
26
- # Checkbox to toggle conversation history display
27
- show_history = st.checkbox("Show conversation history", value=True)
28
 
29
- # Button to trigger response generation
30
  if st.button("Send"):
31
  if user_message:
32
  # Get response from the chatbot
33
  bot_response = converse(user_message)
34
 
 
 
 
 
35
  if show_history:
36
- # Display conversation history if checkbox is checked
37
  st.write("### Conversation History")
38
- st.text(st.session_state.conversation_history)
39
  else:
40
  # Show a warning if no message is provided
41
  st.warning("Please enter a message before sending.")
 
 
 
 
 
 
 
 
 
2
  import streamlit as st
3
  from transformers import pipeline
4
 
5
+ # Caching the model pipeline
6
+ @st.cache_resource
7
+ def load_pipeline():
8
+ return pipeline("text2text-generation", model="facebook/blenderbot-400M-distill")
9
 
10
+ # Initialize the model once using cache
11
+ pipe = load_pipeline()
12
+
13
+ # Initialize session state for conversation history and bot response
14
  if 'conversation_history' not in st.session_state:
15
  st.session_state.conversation_history = ""
16
+ if 'bot_response' not in st.session_state:
17
+ st.session_state.bot_response = ""
18
 
19
  def converse(user_message):
20
  # Update the conversation history
21
  st.session_state.conversation_history += f"User: {user_message}\n"
22
  result = pipe(st.session_state.conversation_history)[0]['generated_text']
23
  st.session_state.conversation_history += f"Bot: {result}\n"
24
+ st.session_state.bot_response = result
25
  return result
26
 
27
+ # Sidebar options
28
+ st.sidebar.title("App Settings")
29
+ show_history = st.sidebar.checkbox("Show conversation history", value=True)
30
+ character_limit = st.sidebar.slider("Set character limit for input:", min_value=50, max_value=500, value=200)
31
+
32
+ # Session reset button
33
+ if st.sidebar.button("Reset Conversation"):
34
+ st.session_state.conversation_history = ""
35
+ st.session_state.bot_response = ""
36
+ st.sidebar.success("Conversation history cleared.")
37
 
38
+ # Streamlit app layout
39
+ st.title("🤖 AI Chatbot")
40
+ st.subheader("Chat with an AI-powered bot!")
41
 
42
+ # Input field with character limit
43
+ user_message = st.text_input(f"Enter your message (max {character_limit} characters):", max_chars=character_limit)
44
 
45
+ # Send button to generate bot response
46
  if st.button("Send"):
47
  if user_message:
48
  # Get response from the chatbot
49
  bot_response = converse(user_message)
50
 
51
+ # Display bot's response in a dedicated area
52
+ st.markdown("### Bot's Response")
53
+ st.success(bot_response)
54
+
55
  if show_history:
56
+ # Display conversation history in a text area for better scrolling
57
  st.write("### Conversation History")
58
+ st.text_area("Conversation", value=st.session_state.conversation_history, height=250, max_chars=None)
59
  else:
60
  # Show a warning if no message is provided
61
  st.warning("Please enter a message before sending.")
62
+
63
+ # About section
64
+ st.markdown("---")
65
+ st.markdown("### About this App")
66
+ st.info("This chatbot is powered by a pre-trained model from the Hugging Face Transformers library. You can chat with the bot, and the conversation history will be maintained during the session.")
67
+
68
+ st.sidebar.markdown("---")
69
+ st.sidebar.write("Created by [Your Name](https://github.com/yourprofile)")