Spaces:
Sleeping
Sleeping
Update app.py
Browse files74 unmodified lines7576777879808182838485862 unmodified lines89909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291 unmodified line13113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318474 unmodified linesmargin-bottom: 1.5rem;}/* Chat container */.container {max-width: 800px;margin: 0 auto;padding: 1rem;}.chat-history {max-height: 450px;overflow-y: auto;2 unmodified linespadding: 1rem;border: 1px solid #DDD;margin-bottom: 1rem;}.message-user {background-color: #DDF3FF;padding: 0.8rem;border-radius: 12px;margin-bottom: 0.5rem;text-align: left;}.message-bot {background-color: #E3FFE1;padding: 0.8rem;border-radius: 12px;margin-bottom: 0.5rem;text-align: left;}.stTextInput > div > input {font-size: 1rem;padding: 0.7rem;border-radius: 8px;}.stButton>button {border-radius: 8px;background-color: #4A90E2;color: white;/* Sidebar image center */.sidebar-logo {text-align: center;margin-bottom: 1rem;}</style>""", unsafe_allow_html=True)# ------------- Sidebar -------------with st.sidebar:st.markdown("<div class='sidebar-logo'><img src='https://cdn-icons-png.flaticon.com/512/4712/4712027.png' width='100'></div>", unsafe_allow_html=True)1 unmodified linest.write("β’ Upload a PDF π")st.write("β’ Ask context-specific questions π¬")st.write("β’ Powered by **LLaMA 3 + Groq** β‘")if st.button("ποΈ Clear Chat"):st.session_state.chat_history = []st.rerun()# ------------- Title -------------st.markdown("<div class='title'>π Multimodal Chat Assistant</div>", unsafe_allow_html=True)with st.container():st.markdown("<div class='container'>", unsafe_allow_html=True)# Upload UIuploaded_file = st.file_uploader("π€ Upload a PDF file", type=["pdf"])if uploaded_file:with st.spinner("π Ingesting document..."):ingest_pdf(uploaded_file.read(), uploaded_file.name)st.success("β Document ingested. Ask away!")# Initialize chat history if not presentif "chat_history" not in st.session_state:st.session_state.chat_history = []# Display chat history first (above input)st.markdown("<div class='chat-history'>", unsafe_allow_html=True)for msg in st.session_state.chat_history:if msg["role"] == "user":st.markdown(f"<div class='message-user'><b>π§ You:</b> {msg['content']}</div>", unsafe_allow_html=True)else:st.markdown(f"<div class='message-bot'><b>π€ Bot:</b> {msg['content']}</div>", unsafe_allow_html=True)st.markdown("</div>", unsafe_allow_html=True)# Prompt input + Send buttoncol1, col2 = st.columns([5, 1])with col1:prompt = st.text_input("π¬ Type your question", placeholder="Ask something about your PDF...")with col2:send_clicked = st.button("β‘οΈ")# Handle inputif (prompt and not send_clicked) or (send_clicked and prompt):st.session_state.chat_history.append({"role": "user", "content": prompt})with st.spinner("π€ Thinking..."):response = qa_chain.run(prompt)st.session_state.chat_history.append({"role": "assistant", "content": response})st.markdown("</div>", unsafe_allow_html=True)# End chat container74 unmodified lines75767778798081828384858687882 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801 unmodified line18218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824974 unmodified linesmargin-bottom: 1.5rem;}/* Main container */.main-container {max-width: 800px;margin: 0 auto;padding: 1rem;}/* Chat history container */.chat-history {max-height: 450px;overflow-y: auto;2 unmodified linespadding: 1rem;border: 1px solid #DDD;margin-bottom: 1rem;min-height: 200px;}/* Message styling */.message-user {background-color: #DDF3FF;padding: 0.8rem;border-radius: 12px;margin-bottom: 0.5rem;text-align: left;word-wrap: break-word;}.message-bot {background-color: #E3FFE1;padding: 0.8rem;border-radius: 12px;margin-bottom: 0.5rem;text-align: left;word-wrap: break-word;}/* Input styling */.stTextInput > div > div > input {font-size: 1rem;padding: 0.7rem;border-radius: 8px;border: 1px solid #DDD;}/* Button styling */.stButton > button {border-radius: 8px;background-color: #4A90E2;color: white;border: none;padding: 0.5rem 1rem;font-weight: bold;width: 100%;height: 100%;}.stButton > button:hover {background-color: #357ABD;}/* File uploader styling */.stFileUploader > div {padding: 1rem;border: 2px dashed #4A90E2;border-radius: 8px;text-align: center;margin-bottom: 1rem;}/* Sidebar styling */.sidebar-logo {text-align: center;margin-bottom: 1rem;}/* Input row alignment */.input-row {display: flex;gap: 0.5rem;align-items: end;margin-top: 1rem;}/* Success message styling */.stSuccess {margin-bottom: 1rem;}/* Empty chat message */.empty-chat {text-align: center;color: #888;font-style: italic;padding: 2rem;}</style>""", unsafe_allow_html=True)# ------------- Sidebar -------------with st.sidebar:st.markdown("<div class='sidebar-logo'><img src='https://cdn-icons-png.flaticon.com/512/4712/4712027.png' width='100'></div>", unsafe_allow_html=True)1 unmodified linest.write("β’ Upload a PDF π")st.write("β’ Ask context-specific questions π¬")st.write("β’ Powered by **LLaMA 3 + Groq** β‘")st.markdown("---")if st.button("ποΈ Clear Chat", use_container_width=True):st.session_state.chat_history = []st.rerun()# ------------- Main Content -------------# Titlest.markdown("<div class='title'>π Multimodal Chat Assistant</div>", unsafe_allow_html=True)# Main containerst.markdown("<div class='main-container'>", unsafe_allow_html=True)# Upload UIuploaded_file = st.file_uploader("π€ Upload a PDF file", type=["pdf"])if uploaded_file:with st.spinner("π Ingesting document..."):ingest_pdf(uploaded_file.read(), uploaded_file.name)st.success("β Document ingested successfully! You can now ask questions about the document.")# Initialize chat history if not presentif "chat_history" not in st.session_state:st.session_state.chat_history = []# Display chat historyst.markdown("<div class='chat-history'>", unsafe_allow_html=True)if st.session_state.chat_history:for msg in st.session_state.chat_history:if msg["role"] == "user":st.markdown(f"<div class='message-user'><b>π§ You:</b> {msg['content']}</div>", unsafe_allow_html=True)else:st.markdown(f"<div class='message-bot'><b>π€ Assistant:</b> {msg['content']}</div>", unsafe_allow_html=True)else:st.markdown("<div class='empty-chat'>No messages yet. Upload a PDF and start asking questions! π</div>", unsafe_allow_html=True)st.markdown("</div>", unsafe_allow_html=True)# Input section with proper alignmentcol1, col2 = st.columns([6, 1])with col1:prompt = st.text_input("π¬ Type your question", placeholder="Ask something about your PDF...", label_visibility="collapsed")with col2:send_clicked = st.button("Send β‘οΈ", use_container_width=True)# Handle input and responseif (prompt and send_clicked) or (prompt and st.session_state.get('last_prompt', '') != prompt):if prompt.strip(): # Only process non-empty prompts# Add user messagest.session_state.chat_history.append({"role": "user", "content": prompt})# Get responsewith st.spinner("π€ Thinking..."):try:response = qa_chain.run(prompt)st.session_state.chat_history.append({"role": "assistant", "content": response})except Exception as e:error_msg = f"Sorry, I encountered an error: {str(e)}"st.session_state.chat_history.append({"role": "assistant", "content": error_msg})# Store last prompt to avoid duplicatesst.session_state.last_prompt = promptst.rerun()st.markdown("</div>", unsafe_allow_html=True)
app.py+108-43