KB-Infinity-Tech commited on
Commit
01176da
ยท
verified ยท
1 Parent(s): aa13ec6

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 pyttsx3
 
8
 
9
  # ----------------------------
10
  # Load Model
@@ -24,13 +25,14 @@ def load_model():
24
  tokenizer, model = load_model()
25
 
26
  # ----------------------------
27
- # TTS (Offline)
28
  # ----------------------------
29
- engine = pyttsx3.init()
 
30
 
31
- def speak(text):
32
- engine.say(text)
33
- engine.runAndWait()
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. Rรฉponds simplement avec des exemples."
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 (Lightweight)
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 DASHBOARD
81
  # ----------------------------
82
  st.set_page_config(layout="wide")
83
- st.title("๐Ÿง ๐Ÿ“Š AI Math Tutor Dashboard")
84
 
85
  col1, col2 = st.columns(2)
86
 
87
  # ----------------------------
88
- # LEFT PANEL โ€” INTERACTION
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
- response = generate(prompt)
 
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
- st.write(f"### ๐Ÿงฎ I see about: {count} objects")
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("๐ŸŽค Voice input simulation (type what child says)")
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 โ€” PROGRESS
152
  # ----------------------------
153
  with col2:
154
- st.header("๐Ÿ“ˆ Learning Progress")
155
 
156
- # Fake metrics (replace with SQLite later)
157
  st.metric("Questions Answered", 12)
158
  st.metric("Accuracy", "75%")
159
  st.metric("Level", "Beginner โ†’ Improving")
160
 
161
- st.subheader("๐Ÿ“Š Skill Breakdown")
162
  st.progress(0.7)
163
 
164
- st.subheader("๐ŸŒ Language Detected")
165
- st.write("Auto-detected per input")
166
 
167
- st.subheader("โšก System Info")
168
- st.write("CPU Mode โœ”")
169
- st.write("Offline-ready โœ”")
170
- st.write("Multilingual โœ”")
 
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")