Download app.py from SNA-AI/AIChatBot: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/SNA-AI/AIChatBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/SNA-AI/AIChatBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SNA-AI/AIChatBot/resolve/main/app.py
1.25 kB
| import streamlit as st | |
| import requests | |
| import os | |
| # 🔐 Get secret from Hugging Face (NOT hardcoded) | |
| API_KEY = os.getenv("GROQ_API_KEY_ChatBot") | |
| API_URL = "https://api.groq.com/openai/v1/chat/completions" | |
| st.set_page_config(page_title="Groq AI Chatbot", layout="centered") | |
| st.title("AI Chatbot") | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Show chat history | |
| for msg in st.session_state.messages: | |
| with st.chat_message(msg["role"]): | |
| st.write(msg["content"]) | |
| def get_response(messages): | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": "llama-3.1-8b-instant", | |
| "messages": messages, | |
| "temperature": 0.7 | |
| } | |
| res = requests.post(API_URL, headers=headers, json=payload) | |
| if res.status_code == 200: | |
| return res.json()["choices"][0]["message"]["content"] | |
| else: | |
| return res.text | |
| user_input = st.chat_input("Type your message...") | |
| if user_input: | |
| st.session_state.messages.append({"role": "user", "content": user_input}) | |
| reply = get_response(st.session_state.messages) | |
| st.session_state.messages.append({"role": "assistant", "content": reply}) | |
| st.rerun() |