File size: 5,890 Bytes
41aa720 9becdce 593ce63 569b6f9 01176da 9becdce 41aa720 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 01176da 9becdce 569b6f9 9becdce 569b6f9 9becdce 569b6f9 9becdce 593ce63 569b6f9 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce 593ce63 9becdce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
from PIL import Image
import numpy as np
from gtts import gTTS
import tempfile
import re
# ----------------------------
# MODELS
# ----------------------------
MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
@st.cache_resource
def load_llm():
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float32,
device_map="cpu"
)
return tokenizer, model
@st.cache_resource
def load_asr():
return pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
tokenizer, model = load_llm()
asr = load_asr()
# ----------------------------
# MULTILINGUAL DETECTION
# ----------------------------
LANG_WORDS = {
"en": ["one", "two", "three", "four", "five"],
"fr": ["un", "deux", "trois", "quatre", "cinq"],
"sw": ["moja", "mbili", "tatu", "nne", "tano"],
"kin": ["imwe", "ebyiri", "eshatu", "enye", "eshanu"]
}
def detect_mixed_language(text):
text = text.lower()
scores = {lang: 0 for lang in LANG_WORDS}
for lang, words in LANG_WORDS.items():
for w in words:
if w in text:
scores[lang] += 1
dominant = max(scores, key=scores.get)
# detect mix
active_langs = [l for l, s in scores.items() if s > 0]
if len(active_langs) > 1:
return dominant, active_langs
else:
return dominant, [dominant]
# ----------------------------
# PROMPT ENGINEERING
# ----------------------------
def build_prompt(user_input, dominant_lang, langs_used):
if dominant_lang == "fr":
base = "Tu es un tuteur de mathรฉmatiques pour enfants. Explique simplement."
elif dominant_lang == "sw":
base = "Wewe ni mwalimu wa hesabu kwa watoto. Eleza kwa urahisi."
elif dominant_lang == "kin":
base = "Uri umwarimu w'imibare ku bana. Sobanura neza."
else:
base = "You are a friendly math tutor for kids. Explain step by step."
# Handle code-switch
if len(langs_used) > 1:
base += " The child used mixed languages. Keep explanation in main language but reuse number words from other language."
return f"{base}\nUser: {user_input}\nAssistant:"
# ----------------------------
# GENERATION
# ----------------------------
def generate(prompt):
inputs = tokenizer(prompt, return_tensors="pt")
output = model.generate(
**inputs,
max_new_tokens=80,
temperature=0.7,
do_sample=True
)
return tokenizer.decode(output[0], skip_special_tokens=True)
# ----------------------------
# TTS (MULTILINGUAL)
# ----------------------------
def speak(text, lang="en"):
lang_map = {
"en": "en",
"fr": "fr",
"sw": "sw",
"kin": "en" # fallback
}
tts = gTTS(text=text, lang=lang_map.get(lang, "en"))
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
tts.save(fp.name)
return fp.name
# ----------------------------
# VISUAL COUNTING (Baseline)
# ----------------------------
def count_objects(image):
img = np.array(image.convert("L"))
binary = img > 128
count = int(binary.sum() / 400)
return max(1, count)
# ----------------------------
# UI
# ----------------------------
st.set_page_config(layout="wide")
st.title("๐ง ๐ Multilingual AI Math Tutor")
col1, col2 = st.columns(2)
# ----------------------------
# LEFT PANEL
# ----------------------------
with col1:
st.header("๐ง Student Interaction")
mode = st.radio("Mode", ["Text", "Voice", "Image"])
# -------- TEXT --------
if mode == "Text":
user_input = st.text_input("Ask or answer:")
if user_input:
dominant, langs = detect_mixed_language(user_input)
prompt = build_prompt(user_input, dominant, langs)
response = generate(prompt)
st.write("### ๐ Answer")
st.write(response)
st.write(f"๐ Dominant: {dominant} | Mixed: {langs}")
if st.button("๐ Speak"):
audio = speak(response, dominant)
st.audio(audio)
# -------- VOICE --------
elif mode == "Voice":
audio_file = st.file_uploader("Upload voice (.wav)", type=["wav", "mp3"])
if audio_file:
result = asr(audio_file)
text = result["text"]
st.write(f"๐ฃ๏ธ Detected: {text}")
dominant, langs = detect_mixed_language(text)
prompt = build_prompt(text, dominant, langs)
response = generate(prompt)
st.write("### ๐ง Response")
st.write(response)
audio = speak(response, dominant)
st.audio(audio)
# -------- IMAGE --------
elif mode == "Image":
uploaded = st.file_uploader("Upload image", type=["png", "jpg"])
if uploaded:
image = Image.open(uploaded)
st.image(image)
count = count_objects(image)
st.write(f"### ๐งฎ I see about {count} objects")
explanation = f"There are {count} objects. Let's count together."
audio = speak(explanation)
st.audio(audio)
# ----------------------------
# RIGHT PANEL (DASHBOARD)
# ----------------------------
with col2:
st.header("๐ Learning Dashboard")
st.metric("Questions", 15)
st.metric("Accuracy", "80%")
st.metric("Level", "Improving")
st.subheader("๐ Skill Progress")
st.progress(0.8)
st.subheader("๐ Language System")
st.write("โ English / French / Swahili / Kinyarwanda")
st.write("โ Code-switch detection")
st.subheader("โก Features")
st.write("โ Voice (Whisper)")
st.write("โ Visual counting")
st.write("โ Multimodal learning") |