Download src/streamlit_app.py from stemeducationmetaverse/schoolchatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/stemeducationmetaverse/schoolchatbot/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/stemeducationmetaverse/schoolchatbot/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/stemeducationmetaverse/schoolchatbot/resolve/main/src/streamlit_app.py
1.87 kB
| import streamlit as st | |
| import os | |
| from huggingface_hub import InferenceClient # Pre-installed native wrapper | |
| # 1. Pull the classic 'Write' token out of your secret settings | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # 2. Fire up the native client using the token | |
| client = InferenceClient(token=HF_TOKEN) | |
| TEACHER_PROMPT = """ | |
| You are a warm, patient, and friendly school teacher helper. | |
| Your goal is to explain things simply so K-12 kids can understand easily. | |
| - Use encouraging words (like "Great question!", "Fantastic!"). | |
| - Keep your sentences short and clear. | |
| - Use fun, relevant emojis to break up text. | |
| - Never give answers to homework directly; instead, guide them step-by-step. | |
| """ | |
| st.title("🤖 My First AI Chatbot") | |
| user_input = st.text_input("Ask something:") | |
| if st.button("Send"): | |
| if not user_input.strip(): | |
| st.warning("Please enter a question.") | |
| elif not HF_TOKEN: | |
| st.error("Your HF_TOKEN secret key is entirely missing from Settings!") | |
| else: | |
| with st.spinner("Processing natively through the cluster..."): | |
| try: | |
| # 3. Route via internal endpoints rather than requests.post | |
| response = client.chat.completions.create( | |
| model="meta-llama/Llama-3.2-1B-Instruct", | |
| messages=[ | |
| {"role": "system", "content": TEACHER_PROMPT}, | |
| {"role": "user", "content": user_input} | |
| ], | |
| max_tokens=50 | |
| ) | |
| # 5. Extract output clean string text safely | |
| answer = response.choices[0].message.content | |
| st.success(answer) | |
| except Exception as e: | |
| # This explicitly unpacks server messages instead of dropping HTML text | |
| st.error(f"System Response: {e}") | |