Download app1.py from VAH6002/CBMB: direct link, hf CLI and curl.
- Browser
- Download file 5.75 kB
-
https://huggingface.co/spaces/VAH6002/CBMB/resolve/main/app1.py
- Command line
-
hf download hf://spaces/VAH6002/CBMB/app1.py
-
curl -L -o app1.py https://huggingface.co/spaces/VAH6002/CBMB/resolve/main/app1.py
5.75 kB
| import streamlit as st | |
| from backend import generate_response | |
| # Streamlit Page Configuration | |
| st.set_page_config(page_title="College Chatbot", layout="centered") # 'centered' is better for mobile | |
| st.title("🎓 CBIT Engineering College Chatbot") | |
| # Sidebar for User Type Selection (Collapsible for Mobile) | |
| with st.sidebar: | |
| user_type = st.selectbox("Select User Type", ["Student", "Parent", "General Public"]) | |
| # Initialize Chat History and session state variables | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| if "user_input" not in st.session_state: | |
| st.session_state.user_input = "" | |
| if "should_clear_input" not in st.session_state: | |
| st.session_state.should_clear_input = False | |
| # Clear input if flag is set | |
| if st.session_state.should_clear_input: | |
| st.session_state.user_input = "" | |
| st.session_state.should_clear_input = False | |
| # Apply mobile-friendly styling | |
| st.markdown( | |
| """ | |
| <style> | |
| /* Optimize for mobile view */ | |
| @media (max-width: 768px) { | |
| .stApp { padding: 10px; } | |
| .stTextInput > div > div { width: 100% !important; } | |
| .stButton > button { width: 100%; font-size: 16px; } | |
| .stMarkdown { font-size: 14px; } | |
| } | |
| /* Chat message styling */ | |
| .chat-bubble { | |
| padding: 10px; | |
| border-radius: 10px; | |
| margin-bottom: 5px; | |
| max-width: 80%; | |
| } | |
| .user-message { background-color: #d1e7dd; align-self: flex-end; } | |
| .bot-message { background-color: #f8d7da; align-self: flex-start; } | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Form for handling input submission | |
| with st.form(key="chat_form"): | |
| user_query = st.text_input( | |
| "Ask me anything about the college...", | |
| value=st.session_state.user_input, | |
| key="user_input" | |
| ) | |
| submit_button = st.form_submit_button("Send") | |
| if submit_button and user_query: | |
| # Generate Response | |
| bot_response = generate_response(user_query, user_type.lower()) | |
| # Store chat history | |
| st.session_state.chat_history.append(("You", user_query)) | |
| st.session_state.chat_history.append(("Chatbot", bot_response)) | |
| # Set flag to clear input and rerun | |
| st.session_state.should_clear_input = True | |
| st.rerun() | |
| # Display Chat History in Reverse Order (Latest messages at the top) | |
| st.subheader("Chat History") | |
| for role, message in reversed(st.session_state.chat_history): | |
| role_class = "user-message" if role == "You" else "bot-message" | |
| st.markdown(f'<div class="chat-bubble {role_class}">{message}</div>', unsafe_allow_html=True) | |
| # import streamlit as st | |
| # from backend import generate_response | |
| # # Streamlit Page Configuration | |
| # st.set_page_config(page_title="College Chatbot", layout="wide") | |
| # st.title("🎓 CBIT Engineering College(Pallavolu) Chatbot") | |
| # # Sidebar for User Type Selection | |
| # user_type = st.sidebar.selectbox( | |
| # "Select User Type", ["Student", "Parent", "General Public"] | |
| # ) | |
| # # Initialize Chat History | |
| # if "chat_history" not in st.session_state: | |
| # st.session_state.chat_history = [] | |
| # # Workaround for Clearing Input Field | |
| # if "user_input" not in st.session_state: | |
| # st.session_state.user_input = "" | |
| # # Form for handling input submission | |
| # with st.form(key="chat_form"): | |
| # user_query = st.text_input("Ask me anything about the college...", value=st.session_state.user_input, key="user_input") | |
| # submit_button = st.form_submit_button("Send") | |
| # if submit_button and user_query: | |
| # # Generate Response | |
| # bot_response = generate_response(user_query, user_type.lower()) | |
| # # Store chat history | |
| # st.session_state.chat_history.append(("You", user_query)) | |
| # st.session_state.chat_history.append(("Chatbot", bot_response)) | |
| # # Clear input field workaround by resetting session state | |
| # st.session_state.user_input = "" | |
| # st.rerun() # Forces UI to refresh and clear input | |
| # # Display Chat History in Reverse Order | |
| # st.subheader("Chat History") | |
| # for role, message in reversed(st.session_state.chat_history): # Reversed order | |
| # with st.chat_message(role): | |
| # st.markdown(message) | |
| # # import streamlit as st | |
| # # from backend import generate_response | |
| # # # Streamlit Page Configuration | |
| # # st.set_page_config(page_title="College Chatbot", layout="centered") | |
| # # st.title("🎓 CBIT Engineering College (Pallavolu) Chatbot") | |
| # # # Sidebar for User Type Selection | |
| # # user_type = st.sidebar.selectbox( | |
| # # "Select User Type", ["Student", "Parent", "General Public"] | |
| # # ) | |
| # # # Chat Input and History | |
| # # if "chat_history" not in st.session_state: | |
| # # st.session_state.chat_history = [] | |
| # # # Display chat messages in reverse order | |
| # # # for msg in reversed(st.session_state.messages): | |
| # # # st.chat_message(msg["role"]).write(msg["content"]) | |
| # # # User Input | |
| # # # user_query = st.text_input("Ask me anything about the CBIT college...", "") | |
| # # if "user_query" not in st.session_state: | |
| # # st.session_state.user_query = "" | |
| # # user_query = st.text_input("Ask me anything about the CBIT college...", value=st.session_state.user_query, key="user_query") | |
| # # if st.button("Send"): | |
| # # if user_query: | |
| # # # Generate Response | |
| # # bot_response = generate_response(user_query, user_type.lower()) | |
| # # # Store chat history | |
| # # st.session_state.chat_history.append(("You", user_query)) | |
| # # st.session_state.chat_history.append(("Chatbot", bot_response)) | |
| # # # Clear userquery | |
| # # st.session_state.user_query = "" | |
| # # st.rerun() | |
| # # # Display Chat History | |
| # # st.subheader("Chat History") | |
| # # for role, message in reversed(st.session_state.chat_history): # Reversed order | |
| # # with st.chat_message(role): | |
| # # st.markdown(message) | |