Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,48 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# -----------------------------
|
| 6 |
-
# Initialize model
|
| 7 |
-
# -----------------------------
|
| 8 |
-
@st.cache_resource(show_spinner=True)
|
| 9 |
-
def load_model(model_name):
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
| 12 |
-
return tokenizer, model
|
| 13 |
-
|
| 14 |
-
# You can choose any LLaMA variant on Hugging Face
|
| 15 |
-
MODEL_NAME = "meta-llama/Llama-2-7b-chat-hf"
|
| 16 |
-
|
| 17 |
-
tokenizer, model = load_model(MODEL_NAME)
|
| 18 |
-
|
| 19 |
-
# -----------------------------
|
| 20 |
-
# Helper function for inference
|
| 21 |
-
# -----------------------------
|
| 22 |
-
def generate_answer(prompt, max_length=256, temperature=0.7):
|
| 23 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 24 |
-
output = model.generate(
|
| 25 |
-
**inputs,
|
| 26 |
-
max_new_tokens=max_length,
|
| 27 |
-
do_sample=True,
|
| 28 |
-
temperature=temperature,
|
| 29 |
-
top_p=0.95,
|
| 30 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 31 |
-
)
|
| 32 |
-
answer = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 33 |
-
return answer
|
| 34 |
-
|
| 35 |
-
# -----------------------------
|
| 36 |
-
# Streamlit UI
|
| 37 |
-
# -----------------------------
|
| 38 |
-
st.set_page_config(page_title="Adaptive AI Learning", layout="wide")
|
| 39 |
-
st.title("📚 Adaptive AI Learning App")
|
| 40 |
-
|
| 41 |
-
if "score" not in st.session_state:
|
| 42 |
-
st.session_state.score = 0
|
| 43 |
-
|
| 44 |
-
st.write("Ask any question to learn!")
|
| 45 |
-
|
| 46 |
-
# User input
|
| 47 |
user_input = st.text_input("Your Question:")
|
| 48 |
-
|
| 49 |
if st.button("Submit") and user_input:
|
| 50 |
-
with st.spinner("AI
|
| 51 |
-
|
| 52 |
st.session_state.last_question = user_input
|
| 53 |
-
st.session_state.last_answer =
|
| 54 |
-
st.success("Answer Generated!")
|
| 55 |
|
| 56 |
-
if
|
| 57 |
st.subheader("AI Answer:")
|
| 58 |
st.write(st.session_state.last_answer)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.
|
| 64 |
-
|
| 65 |
-
st.session_state.score
|
| 66 |
-
if st.button("I didn't understand"):
|
| 67 |
-
st.session_state.score -= 1
|
| 68 |
-
|
| 69 |
st.metric(label="Learning Score", value=st.session_state.score)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
HF_API_TOKEN = st.secrets["HF_API_TOKEN"]
|
| 5 |
+
MODEL_ID = "meta-llama/Llama-2-7b-chat-hf"
|
| 6 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 7 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 8 |
+
|
| 9 |
+
def query_hf_model(prompt, max_length=256):
|
| 10 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": max_length, "temperature":0.7}}
|
| 11 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
|
| 12 |
+
if response.status_code == 200:
|
| 13 |
+
result = response.json()
|
| 14 |
+
if isinstance(result, list) and "generated_text" in result[0]:
|
| 15 |
+
return result[0]["generated_text"]
|
| 16 |
+
elif "error" in result:
|
| 17 |
+
return "Error from model: " + result["error"]
|
| 18 |
+
else:
|
| 19 |
+
return str(result)
|
| 20 |
+
else:
|
| 21 |
+
return f"Request failed: {response.status_code}"
|
| 22 |
+
|
| 23 |
+
st.set_page_config(page_title="AI Adaptive Learning", layout="wide")
|
| 24 |
+
st.title("📚 AI Adaptive Learning App")
|
| 25 |
+
|
| 26 |
+
# Session state
|
| 27 |
+
if "score" not in st.session_state: st.session_state.score = 0
|
| 28 |
+
if "last_question" not in st.session_state: st.session_state.last_question = ""
|
| 29 |
+
if "last_answer" not in st.session_state: st.session_state.last_answer = ""
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
user_input = st.text_input("Your Question:")
|
|
|
|
| 32 |
if st.button("Submit") and user_input:
|
| 33 |
+
with st.spinner("Generating AI answer..."):
|
| 34 |
+
answer = query_hf_model(user_input)
|
| 35 |
st.session_state.last_question = user_input
|
| 36 |
+
st.session_state.last_answer = answer
|
|
|
|
| 37 |
|
| 38 |
+
if st.session_state.last_answer:
|
| 39 |
st.subheader("AI Answer:")
|
| 40 |
st.write(st.session_state.last_answer)
|
| 41 |
|
| 42 |
+
st.subheader("Your Adaptive Learning Score")
|
| 43 |
+
col1, col2 = st.columns(2)
|
| 44 |
+
with col1:
|
| 45 |
+
if st.button("I understood this"): st.session_state.score += 1
|
| 46 |
+
with col2:
|
| 47 |
+
if st.button("I didn't understand"): st.session_state.score -= 1
|
|
|
|
|
|
|
|
|
|
| 48 |
st.metric(label="Learning Score", value=st.session_state.score)
|