Spaces:
Sleeping
Sleeping
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import streamlit as stfrom hugchat import hugchatfrom hugchat.login import Login
# App titlest.set_page_config(page_title="π€π¬ HugChat")
# Hugging Face Credentialswith st.sidebar: st.title('π€π¬ HugChat') if ('EMAIL' in st.secrets) and ('PASS' in st.secrets): st.success('HuggingFace Login credentials already provided!', icon='β
') hf_email = st.secrets['EMAIL'] hf_pass = st.secrets['PASS'] else: hf_email = st.text_input('Enter E-mail:', type='password') hf_pass = st.text_input('Enter password:', type='password') if not (hf_email and hf_pass): st.warning('Please enter your credentials!', icon='β οΈ') else: st.success('Proceed to entering your prompt message!', icon='π') st.markdown('π Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-an-llm-powered-chatbot-with-streamlit/)!') # Store LLM generated responsesif "messages" not in st.session_state.keys(): st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
# Display chat messagesfor message in st.session_state.messages: with st.chat_message(message["role"]): st.write(message["content"])
# Function for generating LLM responsedef generate_response(prompt_input, email, passwd): # Hugging Face Login sign = Login(email, passwd) cookies = sign.login() # Create ChatBot chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) return chatbot.chat(prompt_input)
# User-provided promptif prompt := st.chat_input(disabled=not (hf_email and hf_pass)): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.write(prompt)
# Generate a new response if last message is not from assistantif st.session_state.messages[-1]["role"] != "assistant": with st.chat_message("assistant"): with st.spinner("Thinking..."): response = generate_response(prompt, hf_email, hf_pass) st.write(response) message = {"role": "assistant", "content": response} st.session_state.messages.append(message)app.pyAdded+55