Spaces:
Runtime error
Runtime error
Download app.py from muhammadfaheem34/SimpleChatbot: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/muhammadfaheem34/SimpleChatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/muhammadfaheem34/SimpleChatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/muhammadfaheem34/SimpleChatbot/resolve/main/app.py
2.08 kB
| import streamlit as st | |
| from groq import Groq | |
| import os | |
| # ------------------------- | |
| # π API KEY (Hugging Face Secret) | |
| # ------------------------- | |
| api_key = os.getenv("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") | |
| # β DEFINE CLIENT HERE | |
| client = Groq(api_key=api_key) | |
| # ------------------------- | |
| # PAGE CONFIG | |
| # ------------------------- | |
| st.set_page_config( | |
| page_title="Groq Chatbot", | |
| page_icon="π€", | |
| layout="centered" | |
| ) | |
| # ------------------------- | |
| # UI STYLE (MODERN DARK) | |
| # ------------------------- | |
| st.markdown(""" | |
| <style> | |
| body { | |
| background-color: #0e1117; | |
| color: white; | |
| } | |
| .stChatMessage { | |
| border-radius: 10px; | |
| padding: 10px; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # ------------------------- | |
| # SESSION STATE | |
| # ------------------------- | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # ------------------------- | |
| # SIDEBAR | |
| # ------------------------- | |
| with st.sidebar: | |
| st.title("βοΈ Settings") | |
| model = st.selectbox( | |
| "Model", | |
| ["llama-3.1-70b-versatile" ,"llama-3.1-8b-instant" , "mixtral-8x7b-32768" ] | |
| ) | |
| if st.button("π Clear Chat"): | |
| st.session_state.messages = [] | |
| st.rerun() | |
| # ------------------------- | |
| # MAIN UI | |
| # ------------------------- | |
| st.title("π€ Simple AI Assistant") | |
| # Show messages | |
| for msg in st.session_state.messages: | |
| st.chat_message(msg["role"]).write(msg["content"]) | |
| # Input | |
| user_input = st.chat_input("Type your message...") | |
| if user_input: | |
| st.session_state.messages.append({"role": "user", "content": user_input}) | |
| st.chat_message("user").write(user_input) | |
| with st.spinner("Thinking..."): | |
| try: | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=st.session_state.messages | |
| ) | |
| reply = response.choices[0].message.content | |
| except Exception as e: | |
| reply = f"Error: {str(e)}" | |
| st.session_state.messages.append({"role": "assistant", "content": reply}) | |
| st.chat_message("assistant").write(reply) |