AIbyKaindu commited on
Commit
2ff5579
Β·
verified Β·
1 Parent(s): 309e1cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -55
app.py CHANGED
@@ -10,108 +10,122 @@ if "xp" not in st.session_state:
10
  st.session_state.level = 1
11
  init_leaderboard()
12
 
13
- # New smarter models
14
- MODEL_URLS = {
15
- "Mistral 7B Instruct (High quality)": "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
16
- "Falcon 7B Instruct (Good Reasoning)": "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct",
17
- "Falcon RW-1B (Fast Lightweight)": "https://api-inference.huggingface.co/models/tiiuae/falcon-rw-1b",
18
- }
19
-
20
- def query_model(api_url, prompt):
21
  headers = {"Authorization": f"Bearer {os.environ['HF_Token']}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  payload = {"inputs": prompt}
 
 
 
23
  try:
24
- response = requests.post(api_url, headers=headers, json=payload)
25
- result = response.json()
26
  if isinstance(result, list) and "generated_text" in result[0]:
27
  return result[0]["generated_text"]
28
- return "⚠️ Unexpected response format. Try another topic."
 
29
  except Exception as e:
30
  return f"⚠️ API Error: {e}"
31
 
32
- def update_xp(points):
33
- st.session_state.xp += points
34
- if st.session_state.xp >= 100:
35
- st.session_state.level += 1
36
- st.session_state.xp = 0
37
-
38
- def get_simulation_embed(topic):
39
- topic_map = {
40
  "projectile motion": "https://phet.colorado.edu/sims/html/projectile-motion/latest/projectile-motion_en.html",
41
  "forces": "https://phet.colorado.edu/sims/html/forces-and-motion-basics/latest/forces-and-motion-basics_en.html",
42
- "wave": "https://phet.colorado.edu/sims/html/waves-intro/latest/waves-intro_en.html"
 
 
43
  }
44
- for key in topic_map:
45
  if key in topic.lower():
46
- return topic_map[key]
47
  return "https://phet.colorado.edu/en/simulations/category/physics"
48
 
49
- # --- Streamlit UI ---
 
 
 
 
 
 
50
  st.set_page_config(page_title="CoreQuest Galaxy 🌌", page_icon="🎯")
51
  st.title("🎯 CoreQuest: Master Physics and Math!")
 
52
 
53
- st.sidebar.header("🧠 Pick Your AI Model")
54
- selected_model_name = st.sidebar.selectbox("Choose your AI brain:", list(MODEL_URLS.keys()))
55
- api_url = MODEL_URLS[selected_model_name]
56
-
57
- st.sidebar.markdown("---")
58
  show_leaderboard()
59
 
60
  topic = st.text_input("πŸ“˜ Enter a Physics or Math Topic:")
61
 
62
  if topic:
63
- with st.spinner("Asking the galaxy..."):
64
- prompt = f"Explain '{topic}' for a high school student in a simple way and generate 3 practice questions."
65
- output = query_model(api_url, prompt)
66
 
67
  st.markdown("### πŸ€– AI Response")
68
  st.write(output)
69
 
70
  update_xp(10)
71
  st.progress(st.session_state.xp)
72
- st.write(f"πŸ† Level {st.session_state.level} | XP: {st.session_state.xp} points")
73
 
74
  st.markdown("---")
75
- st.subheader("πŸ”§ Actions")
 
 
 
76
 
77
- if st.button("πŸ’¬ Ask Standard AI"):
78
- user_question = st.text_input("Your follow-up question:", key="chat_input")
79
- if user_question:
80
- with st.spinner("Thinking..."):
81
- answer = query_model(api_url, f"Answer this: {user_question}")
82
- st.success(answer)
83
- update_xp(5)
84
 
85
- if st.button("🌌 Ask Learning Galaxy GPT (with Notes)"):
86
- galaxy_question = st.text_input("Ask Learning Galaxy:", key="galaxy_input")
87
  if galaxy_question:
88
  with st.spinner("Learning Galaxy thinking..."):
89
  galaxy_answer = learning_galaxy_answer(galaxy_question)
90
  st.success(galaxy_answer)
91
  update_xp(10)
92
 
93
- if st.button("πŸ”­ View Simulation"):
94
- sim_url = get_simulation_embed(topic)
95
- st.components.v1.iframe(sim_url, height=500)
96
- update_xp(5)
97
-
98
- if st.button("🎁 See Hint"):
99
- st.info("Hint: Sketch diagrams or list basic principles related to the topic!")
100
  update_xp(3)
101
 
102
- if st.button("πŸ”„ Get More Practice"):
103
- with st.spinner("Getting more practice..."):
104
- new_output = query_model(api_url, prompt)
105
  st.write(new_output)
106
  update_xp(7)
107
 
108
  st.markdown("---")
109
- st.subheader("πŸ… Submit Your Score")
110
  player_name = st.text_input("πŸ‘€ Enter your name to save XP:")
111
  if st.button("πŸ† Save to Leaderboard"):
112
  update_leaderboard(player_name, st.session_state.xp)
113
- st.success("Score submitted successfully!")
114
  show_leaderboard()
115
 
116
  st.markdown("---")
117
- st.caption("Created with ❀️ | CoreQuest Galaxy πŸš€ Powered by Hugging Face + Learning Galaxy GPT ✨")
 
10
  st.session_state.level = 1
11
  init_leaderboard()
12
 
13
+ # Model setup
14
+ MODEL_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
15
+
16
+ def enhanced_query_model(topic):
 
 
 
 
17
  headers = {"Authorization": f"Bearer {os.environ['HF_Token']}"}
18
+
19
+ prompt = f"""
20
+ You are a physics and mathematics AI tutor.
21
+
22
+ Given the topic: "{topic}"
23
+
24
+ 1. Provide a simple and clear explanation suitable for high school or college students.
25
+ 2. Create 3 meaningful practice questions to test their understanding.
26
+ 3. Suggest an appropriate simulation (either a description or a real link) to help visualize the concept.
27
+
28
+ Format:
29
+ ---
30
+ Explanation:
31
+ ...
32
+
33
+ Questions:
34
+ 1.
35
+ 2.
36
+ 3.
37
+
38
+ Suggested Simulation:
39
+ ...
40
+ ---
41
+ """
42
+
43
  payload = {"inputs": prompt}
44
+ response = requests.post(MODEL_URL, headers=headers, json=payload)
45
+ result = response.json()
46
+
47
  try:
 
 
48
  if isinstance(result, list) and "generated_text" in result[0]:
49
  return result[0]["generated_text"]
50
+ else:
51
+ return "⚠️ Unexpected format. Try another topic."
52
  except Exception as e:
53
  return f"⚠️ API Error: {e}"
54
 
55
+ def find_simulation_link(topic):
56
+ sim_map = {
 
 
 
 
 
 
57
  "projectile motion": "https://phet.colorado.edu/sims/html/projectile-motion/latest/projectile-motion_en.html",
58
  "forces": "https://phet.colorado.edu/sims/html/forces-and-motion-basics/latest/forces-and-motion-basics_en.html",
59
+ "electric circuits": "https://phet.colorado.edu/sims/html/circuit-construction-kit-dc/latest/circuit-construction-kit-dc_en.html",
60
+ "waves": "https://phet.colorado.edu/sims/html/waves-intro/latest/waves-intro_en.html",
61
+ "optics": "https://phet.colorado.edu/sims/html/bending-light/latest/bending-light_en.html",
62
  }
63
+ for key in sim_map:
64
  if key in topic.lower():
65
+ return sim_map[key]
66
  return "https://phet.colorado.edu/en/simulations/category/physics"
67
 
68
+ def update_xp(points):
69
+ st.session_state.xp += points
70
+ if st.session_state.xp >= 100:
71
+ st.session_state.level += 1
72
+ st.session_state.xp = 0
73
+
74
+ # Streamlit UI
75
  st.set_page_config(page_title="CoreQuest Galaxy 🌌", page_icon="🎯")
76
  st.title("🎯 CoreQuest: Master Physics and Math!")
77
+ st.caption("Learn with AI tutors, RAG-enhanced answers, XP leveling, and interactive simulations!")
78
 
79
+ st.sidebar.header("πŸ† Leaderboard and XP Tracker")
 
 
 
 
80
  show_leaderboard()
81
 
82
  topic = st.text_input("πŸ“˜ Enter a Physics or Math Topic:")
83
 
84
  if topic:
85
+ with st.spinner("Generating deep explanations and simulations..."):
86
+ output = enhanced_query_model(topic)
 
87
 
88
  st.markdown("### πŸ€– AI Response")
89
  st.write(output)
90
 
91
  update_xp(10)
92
  st.progress(st.session_state.xp)
93
+ st.write(f"πŸ† Level {st.session_state.level} | XP: {st.session_state.xp} Points")
94
 
95
  st.markdown("---")
96
+ st.subheader("πŸ”­ Simulation Area")
97
+
98
+ sim_url = find_simulation_link(topic)
99
+ st.components.v1.iframe(sim_url, height=500)
100
 
101
+ st.markdown("---")
102
+ st.subheader("πŸ”§ Actions")
 
 
 
 
 
103
 
104
+ if st.button("🌌 Ask Learning Galaxy GPT"):
105
+ galaxy_question = st.text_input("Ask a more advanced question:", key="galaxy_input")
106
  if galaxy_question:
107
  with st.spinner("Learning Galaxy thinking..."):
108
  galaxy_answer = learning_galaxy_answer(galaxy_question)
109
  st.success(galaxy_answer)
110
  update_xp(10)
111
 
112
+ if st.button("🎁 See a Hint"):
113
+ st.info("Hint: Sketch diagrams, list formulas, or visualize forces/fields.")
 
 
 
 
 
114
  update_xp(3)
115
 
116
+ if st.button("πŸ”„ Get Another Set of Practice"):
117
+ with st.spinner("Loading more questions..."):
118
+ new_output = enhanced_query_model(topic)
119
  st.write(new_output)
120
  update_xp(7)
121
 
122
  st.markdown("---")
123
+ st.subheader("πŸ… Submit Your XP Score")
124
  player_name = st.text_input("πŸ‘€ Enter your name to save XP:")
125
  if st.button("πŸ† Save to Leaderboard"):
126
  update_leaderboard(player_name, st.session_state.xp)
127
+ st.success("XP submitted!")
128
  show_leaderboard()
129
 
130
  st.markdown("---")
131
+ st.caption("Created with ❀️ by CoreQuest Galaxy πŸš€ Powered by Hugging Face + Learning Galaxy ✨")