Spaces:
Sleeping
Sleeping
Download app.py from DanishShahzad/Sample_ChatBot: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/spaces/DanishShahzad/Sample_ChatBot/resolve/refs%2Fpr%2F1/app.py
- Command line
-
hf download hf://spaces/DanishShahzad/Sample_ChatBot@refs/pr/1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DanishShahzad/Sample_ChatBot/resolve/refs%2Fpr%2F1/app.py
4.78 kB
| # app.py | |
| import streamlit as st | |
| import json | |
| from groq_chat import chat_with_groq, stream_chat_with_groq | |
| st.set_page_config(page_title="Groq Chatbot", page_icon="π€", layout="centered") | |
| st.title("π€ Groq Chatbot (Kimi)") | |
| st.markdown("Chat with Moonshot AI's Kimi model powered by Groq API.") | |
| # --- Session State Initialization --- | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| if "system_prompt" not in st.session_state: | |
| st.session_state.system_prompt = "You are a helpful AI assistant." | |
| if "temperature" not in st.session_state: | |
| st.session_state.temperature = 0.7 | |
| if "show_sidebar" not in st.session_state: | |
| st.session_state.show_sidebar = True | |
| if "theme_pref" not in st.session_state: | |
| st.session_state.theme_pref = "System" | |
| if "use_streaming" not in st.session_state: | |
| st.session_state.use_streaming = True | |
| # --- Sidebar Toggle Button --- | |
| toggle_label = "π Hide Sidebar" if st.session_state.show_sidebar else "π Show Sidebar" | |
| if st.button(toggle_label): | |
| st.session_state.show_sidebar = not st.session_state.show_sidebar | |
| # --- Sidebar --- | |
| if st.session_state.show_sidebar: | |
| with st.sidebar: | |
| st.markdown("## π§ Settings") | |
| st.session_state.temperature = st.slider( | |
| "Temperature", 0.0, 1.0, st.session_state.temperature, 0.05 | |
| ) | |
| st.session_state.system_prompt = st.text_area( | |
| "System Prompt", value=st.session_state.system_prompt | |
| ) | |
| st.session_state.use_streaming = st.toggle( | |
| "β‘ Use Streaming Responses", value=st.session_state.use_streaming | |
| ) | |
| st.session_state.theme_pref = st.radio( | |
| "π Theme Preference", ["Light", "Dark", "System"], | |
| index=["Light", "Dark", "System"].index(st.session_state.theme_pref) | |
| ) | |
| st.markdown("> π‘ Change actual theme via βοΈ in top-right.") | |
| st.markdown("---") | |
| # Chat download | |
| if st.session_state.chat_history: | |
| chat_json = json.dumps(st.session_state.chat_history, indent=2) | |
| st.download_button("πΎ Download Chat", chat_json, file_name="chat_history.json", mime="application/json") | |
| # Chat upload | |
| uploaded = st.file_uploader("π Load Previous Chat", type=["json"]) | |
| if uploaded: | |
| st.session_state.chat_history = json.load(uploaded) | |
| st.success("Chat loaded successfully.") | |
| st.rerun() | |
| # --- Clear Chat Button --- | |
| if st.button("π§Ή Clear Chat"): | |
| st.session_state.chat_history = [] | |
| st.rerun() | |
| # --- Display Previous Chat --- | |
| for msg in st.session_state.chat_history: | |
| avatar = "π€" if msg["role"] == "user" else "π€" | |
| with st.chat_message(msg["role"], avatar=avatar): | |
| st.markdown(msg["content"]) | |
| # --- Chat Input --- | |
| prompt = st.chat_input("Type your message here...") | |
| if prompt: | |
| st.session_state.chat_history.append({"role": "user", "content": prompt}) | |
| with st.chat_message("user", avatar="π€"): | |
| st.markdown(prompt) | |
| with st.chat_message("assistant", avatar="π€"): | |
| with st.spinner("Groq is thinking..."): | |
| full_response = "" | |
| try: | |
| if st.session_state.use_streaming: | |
| stream = stream_chat_with_groq( | |
| prompt, | |
| history=st.session_state.chat_history, | |
| system_prompt=st.session_state.system_prompt, | |
| temperature=st.session_state.temperature | |
| ) | |
| response_container = st.empty() | |
| for chunk in stream: | |
| if chunk.choices and chunk.choices[0].delta.content: | |
| token = chunk.choices[0].delta.content | |
| full_response += token | |
| response_container.markdown(full_response) | |
| st.sidebar.markdown("π **Tokens Used:** Not available in streaming mode.") | |
| else: | |
| response = chat_with_groq( | |
| prompt, | |
| history=st.session_state.chat_history, | |
| system_prompt=st.session_state.system_prompt, | |
| temperature=st.session_state.temperature | |
| ) | |
| full_response = response.choices[0].message.content | |
| st.markdown(full_response) | |
| if "usage" in response: | |
| st.sidebar.markdown(f"π **Tokens Used:** {response['usage']['total_tokens']}") | |
| st.session_state.chat_history.append({"role": "assistant", "content": full_response}) | |
| except Exception as e: | |
| st.error(f"π₯ API Error: {e}") | |