Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,13 @@ import requests
|
|
| 4 |
# -----------------------------
|
| 5 |
# Hugging Face API setup
|
| 6 |
# -----------------------------
|
| 7 |
-
HF_API_TOKEN = st.secrets
|
| 8 |
-
MODEL_ID = "meta-llama/Llama-2-7b-chat-hf"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 11 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 12 |
|
|
@@ -16,34 +20,50 @@ HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
|
| 16 |
def query_hf_model(prompt, max_length=256):
|
| 17 |
payload = {
|
| 18 |
"inputs": prompt,
|
| 19 |
-
"parameters": {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
|
|
|
| 21 |
try:
|
| 22 |
-
response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=
|
|
|
|
| 23 |
if response.status_code == 200:
|
| 24 |
result = response.json()
|
|
|
|
| 25 |
if isinstance(result, list) and "generated_text" in result[0]:
|
| 26 |
-
return result[0]["generated_text"]
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
else:
|
| 32 |
-
return f"Request failed
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error: {e}"
|
|
|
|
| 35 |
|
| 36 |
# -----------------------------
|
| 37 |
# Streamlit UI
|
| 38 |
# -----------------------------
|
| 39 |
st.set_page_config(page_title="AI Adaptive Learning", layout="wide")
|
|
|
|
| 40 |
st.title("📚 AI Adaptive Learning App")
|
| 41 |
-
st.write("Ask a question and get AI-generated explanations.
|
| 42 |
|
| 43 |
-
# Session state
|
| 44 |
-
if "score" not in st.session_state:
|
| 45 |
-
|
| 46 |
-
if "last_answer" not in st.session_state:
|
|
|
|
| 47 |
|
| 48 |
# User input
|
| 49 |
user_input = st.text_input("Your Question:")
|
|
@@ -51,10 +71,9 @@ user_input = st.text_input("Your Question:")
|
|
| 51 |
if st.button("Submit") and user_input:
|
| 52 |
with st.spinner("Generating AI answer..."):
|
| 53 |
answer = query_hf_model(user_input)
|
| 54 |
-
st.session_state.last_question = user_input
|
| 55 |
st.session_state.last_answer = answer
|
| 56 |
|
| 57 |
-
# Display
|
| 58 |
if st.session_state.last_answer:
|
| 59 |
st.subheader("AI Answer:")
|
| 60 |
st.write(st.session_state.last_answer)
|
|
@@ -62,11 +81,13 @@ if st.session_state.last_answer:
|
|
| 62 |
# Adaptive learning score
|
| 63 |
st.subheader("Your Adaptive Learning Score")
|
| 64 |
col1, col2 = st.columns(2)
|
|
|
|
| 65 |
with col1:
|
| 66 |
-
if st.button("I understood this"):
|
| 67 |
st.session_state.score += 1
|
|
|
|
| 68 |
with col2:
|
| 69 |
-
if st.button("I didn't understand"):
|
| 70 |
st.session_state.score -= 1
|
| 71 |
|
| 72 |
st.metric(label="Learning Score", value=st.session_state.score)
|
|
|
|
| 4 |
# -----------------------------
|
| 5 |
# Hugging Face API setup
|
| 6 |
# -----------------------------
|
| 7 |
+
HF_API_TOKEN = st.secrets.get("HF_API_TOKEN")
|
|
|
|
| 8 |
|
| 9 |
+
if not HF_API_TOKEN:
|
| 10 |
+
st.error("HF_API_TOKEN not found in Secrets.")
|
| 11 |
+
st.stop()
|
| 12 |
+
|
| 13 |
+
MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 14 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 15 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 16 |
|
|
|
|
| 20 |
def query_hf_model(prompt, max_length=256):
|
| 21 |
payload = {
|
| 22 |
"inputs": prompt,
|
| 23 |
+
"parameters": {
|
| 24 |
+
"max_new_tokens": max_length,
|
| 25 |
+
"temperature": 0.7,
|
| 26 |
+
"return_full_text": False
|
| 27 |
+
},
|
| 28 |
}
|
| 29 |
+
|
| 30 |
try:
|
| 31 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=120)
|
| 32 |
+
|
| 33 |
if response.status_code == 200:
|
| 34 |
result = response.json()
|
| 35 |
+
|
| 36 |
if isinstance(result, list) and "generated_text" in result[0]:
|
| 37 |
+
return result[0]["generated_text"].strip()
|
| 38 |
+
|
| 39 |
+
if isinstance(result, dict) and "error" in result:
|
| 40 |
+
return "Model error: " + result["error"]
|
| 41 |
+
|
| 42 |
+
return str(result)
|
| 43 |
+
|
| 44 |
+
elif response.status_code == 503:
|
| 45 |
+
return "Model is loading. Please try again in a few seconds."
|
| 46 |
+
|
| 47 |
else:
|
| 48 |
+
return f"Request failed with status code {response.status_code}"
|
| 49 |
+
|
| 50 |
except Exception as e:
|
| 51 |
+
return f"Error: {str(e)}"
|
| 52 |
+
|
| 53 |
|
| 54 |
# -----------------------------
|
| 55 |
# Streamlit UI
|
| 56 |
# -----------------------------
|
| 57 |
st.set_page_config(page_title="AI Adaptive Learning", layout="wide")
|
| 58 |
+
|
| 59 |
st.title("📚 AI Adaptive Learning App")
|
| 60 |
+
st.write("Ask a question and get AI-generated explanations.")
|
| 61 |
|
| 62 |
+
# Session state
|
| 63 |
+
if "score" not in st.session_state:
|
| 64 |
+
st.session_state.score = 0
|
| 65 |
+
if "last_answer" not in st.session_state:
|
| 66 |
+
st.session_state.last_answer = ""
|
| 67 |
|
| 68 |
# User input
|
| 69 |
user_input = st.text_input("Your Question:")
|
|
|
|
| 71 |
if st.button("Submit") and user_input:
|
| 72 |
with st.spinner("Generating AI answer..."):
|
| 73 |
answer = query_hf_model(user_input)
|
|
|
|
| 74 |
st.session_state.last_answer = answer
|
| 75 |
|
| 76 |
+
# Display answer
|
| 77 |
if st.session_state.last_answer:
|
| 78 |
st.subheader("AI Answer:")
|
| 79 |
st.write(st.session_state.last_answer)
|
|
|
|
| 81 |
# Adaptive learning score
|
| 82 |
st.subheader("Your Adaptive Learning Score")
|
| 83 |
col1, col2 = st.columns(2)
|
| 84 |
+
|
| 85 |
with col1:
|
| 86 |
+
if st.button("I understood this 👍"):
|
| 87 |
st.session_state.score += 1
|
| 88 |
+
|
| 89 |
with col2:
|
| 90 |
+
if st.button("I didn't understand 👎"):
|
| 91 |
st.session_state.score -= 1
|
| 92 |
|
| 93 |
st.metric(label="Learning Score", value=st.session_state.score)
|