Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -37
src/streamlit_app.py
CHANGED
|
@@ -4,7 +4,8 @@ import torch
|
|
| 4 |
from langdetect import detect
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
-
import
|
|
|
|
| 8 |
|
| 9 |
# ----------------------------
|
| 10 |
# Load Model
|
|
@@ -24,13 +25,14 @@ def load_model():
|
|
| 24 |
tokenizer, model = load_model()
|
| 25 |
|
| 26 |
# ----------------------------
|
| 27 |
-
#
|
| 28 |
# ----------------------------
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
# ----------------------------
|
| 36 |
# Language Detection
|
|
@@ -46,7 +48,7 @@ def detect_lang(text):
|
|
| 46 |
# ----------------------------
|
| 47 |
def build_prompt(user_input, lang):
|
| 48 |
if lang == "fr":
|
| 49 |
-
system = "Tu es un tuteur de mathรฉmatiques pour enfants.
|
| 50 |
elif lang == "sw":
|
| 51 |
system = "Wewe ni mwalimu wa hesabu kwa watoto. Eleza kwa urahisi."
|
| 52 |
else:
|
|
@@ -68,7 +70,7 @@ def generate(prompt):
|
|
| 68 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 69 |
|
| 70 |
# ----------------------------
|
| 71 |
-
# Visual Counting
|
| 72 |
# ----------------------------
|
| 73 |
def count_objects(image):
|
| 74 |
img = np.array(image.convert("L"))
|
|
@@ -77,21 +79,22 @@ def count_objects(image):
|
|
| 77 |
return max(1, count)
|
| 78 |
|
| 79 |
# ----------------------------
|
| 80 |
-
# UI
|
| 81 |
# ----------------------------
|
| 82 |
st.set_page_config(layout="wide")
|
| 83 |
-
st.title("๐ง ๐ AI Math Tutor
|
| 84 |
|
| 85 |
col1, col2 = st.columns(2)
|
| 86 |
|
| 87 |
# ----------------------------
|
| 88 |
-
# LEFT PANEL โ
|
| 89 |
# ----------------------------
|
| 90 |
with col1:
|
| 91 |
st.header("๐ง Student Interaction")
|
| 92 |
|
| 93 |
mode = st.radio("Choose Mode", ["Text", "Image (Count)", "Voice (Simulated)"])
|
| 94 |
|
|
|
|
| 95 |
if mode == "Text":
|
| 96 |
user_input = st.text_input("Ask a math question:")
|
| 97 |
|
|
@@ -99,17 +102,17 @@ with col1:
|
|
| 99 |
lang = detect_lang(user_input)
|
| 100 |
prompt = build_prompt(user_input, lang)
|
| 101 |
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
st.write("### ๐ Answer")
|
| 105 |
st.write(response)
|
| 106 |
|
| 107 |
if st.button("๐ Speak Answer"):
|
| 108 |
-
speak(response)
|
|
|
|
| 109 |
|
| 110 |
-
# ----------------------------
|
| 111 |
-
# IMAGE MODE (Visual Learning)
|
| 112 |
-
# ----------------------------
|
| 113 |
elif mode == "Image (Count)":
|
| 114 |
uploaded = st.file_uploader("Upload image with objects", type=["png", "jpg"])
|
| 115 |
|
|
@@ -119,52 +122,50 @@ with col1:
|
|
| 119 |
|
| 120 |
count = count_objects(image)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
explanation = f"There are about {count} objects. Let's count together!"
|
| 125 |
-
st.write(explanation)
|
| 126 |
|
| 127 |
if st.button("๐ Speak"):
|
| 128 |
-
speak(explanation)
|
|
|
|
| 129 |
|
| 130 |
-
# ----------------------------
|
| 131 |
-
# VOICE MODE (SIMULATED)
|
| 132 |
-
# ----------------------------
|
| 133 |
elif mode == "Voice (Simulated)":
|
| 134 |
-
st.write("๐ค
|
| 135 |
|
| 136 |
voice_input = st.text_input("Child says:")
|
| 137 |
|
| 138 |
if voice_input:
|
| 139 |
lang = detect_lang(voice_input)
|
| 140 |
-
|
| 141 |
prompt = build_prompt(voice_input, lang)
|
|
|
|
| 142 |
response = generate(prompt)
|
| 143 |
|
| 144 |
st.write("### ๐ง Tutor Response")
|
| 145 |
st.write(response)
|
| 146 |
|
| 147 |
if st.button("๐ Speak Response"):
|
| 148 |
-
speak(response)
|
|
|
|
| 149 |
|
| 150 |
# ----------------------------
|
| 151 |
-
# RIGHT PANEL โ
|
| 152 |
# ----------------------------
|
| 153 |
with col2:
|
| 154 |
-
st.header("๐ Learning
|
| 155 |
|
| 156 |
-
#
|
| 157 |
st.metric("Questions Answered", 12)
|
| 158 |
st.metric("Accuracy", "75%")
|
| 159 |
st.metric("Level", "Beginner โ Improving")
|
| 160 |
|
| 161 |
-
st.subheader("๐ Skill
|
| 162 |
st.progress(0.7)
|
| 163 |
|
| 164 |
-
st.subheader("๐ Language
|
| 165 |
-
st.write("Auto-
|
| 166 |
|
| 167 |
-
st.subheader("โก System
|
| 168 |
-
st.
|
| 169 |
-
st.
|
| 170 |
-
st.
|
|
|
|
| 4 |
from langdetect import detect
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
+
from gtts import gTTS
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
# ----------------------------
|
| 11 |
# Load Model
|
|
|
|
| 25 |
tokenizer, model = load_model()
|
| 26 |
|
| 27 |
# ----------------------------
|
| 28 |
+
# Text-to-Speech (HF SAFE)
|
| 29 |
# ----------------------------
|
| 30 |
+
def speak(text, lang="en"):
|
| 31 |
+
tts = gTTS(text=text, lang=lang)
|
| 32 |
|
| 33 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 34 |
+
tts.save(fp.name)
|
| 35 |
+
return fp.name
|
| 36 |
|
| 37 |
# ----------------------------
|
| 38 |
# Language Detection
|
|
|
|
| 48 |
# ----------------------------
|
| 49 |
def build_prompt(user_input, lang):
|
| 50 |
if lang == "fr":
|
| 51 |
+
system = "Tu es un tuteur de mathรฉmatiques pour enfants. Explique simplement avec des exemples."
|
| 52 |
elif lang == "sw":
|
| 53 |
system = "Wewe ni mwalimu wa hesabu kwa watoto. Eleza kwa urahisi."
|
| 54 |
else:
|
|
|
|
| 70 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 71 |
|
| 72 |
# ----------------------------
|
| 73 |
+
# Visual Counting
|
| 74 |
# ----------------------------
|
| 75 |
def count_objects(image):
|
| 76 |
img = np.array(image.convert("L"))
|
|
|
|
| 79 |
return max(1, count)
|
| 80 |
|
| 81 |
# ----------------------------
|
| 82 |
+
# UI CONFIG
|
| 83 |
# ----------------------------
|
| 84 |
st.set_page_config(layout="wide")
|
| 85 |
+
st.title("๐ง ๐ AI Math Tutor (Multimodal)")
|
| 86 |
|
| 87 |
col1, col2 = st.columns(2)
|
| 88 |
|
| 89 |
# ----------------------------
|
| 90 |
+
# LEFT PANEL โ STUDENT
|
| 91 |
# ----------------------------
|
| 92 |
with col1:
|
| 93 |
st.header("๐ง Student Interaction")
|
| 94 |
|
| 95 |
mode = st.radio("Choose Mode", ["Text", "Image (Count)", "Voice (Simulated)"])
|
| 96 |
|
| 97 |
+
# ---------------- TEXT MODE ----------------
|
| 98 |
if mode == "Text":
|
| 99 |
user_input = st.text_input("Ask a math question:")
|
| 100 |
|
|
|
|
| 102 |
lang = detect_lang(user_input)
|
| 103 |
prompt = build_prompt(user_input, lang)
|
| 104 |
|
| 105 |
+
with st.spinner("Thinking..."):
|
| 106 |
+
response = generate(prompt)
|
| 107 |
|
| 108 |
st.write("### ๐ Answer")
|
| 109 |
st.write(response)
|
| 110 |
|
| 111 |
if st.button("๐ Speak Answer"):
|
| 112 |
+
audio_file = speak(response, lang)
|
| 113 |
+
st.audio(audio_file)
|
| 114 |
|
| 115 |
+
# ---------------- IMAGE MODE ----------------
|
|
|
|
|
|
|
| 116 |
elif mode == "Image (Count)":
|
| 117 |
uploaded = st.file_uploader("Upload image with objects", type=["png", "jpg"])
|
| 118 |
|
|
|
|
| 122 |
|
| 123 |
count = count_objects(image)
|
| 124 |
|
| 125 |
+
explanation = f"I see about {count} objects. Let's count together!"
|
| 126 |
+
st.write(f"### ๐งฎ {explanation}")
|
|
|
|
|
|
|
| 127 |
|
| 128 |
if st.button("๐ Speak"):
|
| 129 |
+
audio_file = speak(explanation)
|
| 130 |
+
st.audio(audio_file)
|
| 131 |
|
| 132 |
+
# ---------------- VOICE MODE ----------------
|
|
|
|
|
|
|
| 133 |
elif mode == "Voice (Simulated)":
|
| 134 |
+
st.write("๐ค Simulate child's speech (type it)")
|
| 135 |
|
| 136 |
voice_input = st.text_input("Child says:")
|
| 137 |
|
| 138 |
if voice_input:
|
| 139 |
lang = detect_lang(voice_input)
|
|
|
|
| 140 |
prompt = build_prompt(voice_input, lang)
|
| 141 |
+
|
| 142 |
response = generate(prompt)
|
| 143 |
|
| 144 |
st.write("### ๐ง Tutor Response")
|
| 145 |
st.write(response)
|
| 146 |
|
| 147 |
if st.button("๐ Speak Response"):
|
| 148 |
+
audio_file = speak(response, lang)
|
| 149 |
+
st.audio(audio_file)
|
| 150 |
|
| 151 |
# ----------------------------
|
| 152 |
+
# RIGHT PANEL โ DASHBOARD
|
| 153 |
# ----------------------------
|
| 154 |
with col2:
|
| 155 |
+
st.header("๐ Learning Dashboard")
|
| 156 |
|
| 157 |
+
# Demo metrics (replace with SQLite later)
|
| 158 |
st.metric("Questions Answered", 12)
|
| 159 |
st.metric("Accuracy", "75%")
|
| 160 |
st.metric("Level", "Beginner โ Improving")
|
| 161 |
|
| 162 |
+
st.subheader("๐ Skill Progress")
|
| 163 |
st.progress(0.7)
|
| 164 |
|
| 165 |
+
st.subheader("๐ Language Handling")
|
| 166 |
+
st.write("Auto-detect + multilingual response")
|
| 167 |
|
| 168 |
+
st.subheader("โก System Status")
|
| 169 |
+
st.success("CPU Mode")
|
| 170 |
+
st.success("Multimodal Enabled")
|
| 171 |
+
st.success("Deployable on Hugging Face")
|