Maira-ghaffar commited on
Commit
8a756a4
·
verified ·
1 Parent(s): 740e21f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -15
app.py CHANGED
@@ -1,48 +1,72 @@
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)
 
1
  import streamlit as st
2
  import requests
3
 
4
+ # -----------------------------
5
+ # Hugging Face API setup
6
+ # -----------------------------
7
+ HF_API_TOKEN = st.secrets["HF_API_TOKEN"] # Add token in Space 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
 
13
+ # -----------------------------
14
+ # Helper function to query model
15
+ # -----------------------------
16
  def query_hf_model(prompt, max_length=256):
17
+ payload = {
18
+ "inputs": prompt,
19
+ "parameters": {"max_new_tokens": max_length, "temperature": 0.7},
20
+ }
21
+ try:
22
+ response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
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
+ elif "error" in result:
28
+ return "Error from model: " + result["error"]
29
+ else:
30
+ return str(result)
31
  else:
32
+ return f"Request failed: {response.status_code}"
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. Track your understanding with a score.")
42
 
43
+ # Session state for adaptive learning
44
  if "score" not in st.session_state: st.session_state.score = 0
45
  if "last_question" not in st.session_state: st.session_state.last_question = ""
46
  if "last_answer" not in st.session_state: st.session_state.last_answer = ""
47
 
48
+ # User input
49
  user_input = st.text_input("Your Question:")
50
+
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 AI answer
58
  if st.session_state.last_answer:
59
  st.subheader("AI Answer:")
60
  st.write(st.session_state.last_answer)
61
 
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)