File size: 1,871 Bytes
29c458d
2e4e672
06c3bfd
 
 
2e4e672
06c3bfd
 
 
 
13bae0d
 
 
 
 
 
 
 
 
 
2e4e672
 
06c3bfd
2e4e672
 
 
06c3bfd
 
2e4e672
06c3bfd
2e4e672
06c3bfd
bcd6472
e6b79b9
3c6f443
 
 
 
bcd6472
06c3bfd
bcd6472
 
903a1cc
2e4e672
06c3bfd
 
 
 
13bae0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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}")