Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"β οΈ API Error: {e}"
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 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 |
-
"
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
-
for key in
|
| 45 |
if key in topic.lower():
|
| 46 |
-
return
|
| 47 |
return "https://phet.colorado.edu/en/simulations/category/physics"
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
st.set_page_config(page_title="CoreQuest Galaxy π", page_icon="π―")
|
| 51 |
st.title("π― CoreQuest: Master Physics and Math!")
|
|
|
|
| 52 |
|
| 53 |
-
st.sidebar.header("
|
| 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("
|
| 64 |
-
|
| 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}
|
| 73 |
|
| 74 |
st.markdown("---")
|
| 75 |
-
st.subheader("
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 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
|
| 86 |
-
galaxy_question = st.text_input("Ask
|
| 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("
|
| 94 |
-
|
| 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
|
| 103 |
-
with st.spinner("
|
| 104 |
-
new_output =
|
| 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("
|
| 114 |
show_leaderboard()
|
| 115 |
|
| 116 |
st.markdown("---")
|
| 117 |
-
st.caption("Created with β€οΈ
|
|
|
|
| 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 β¨")
|