Update src/streamlit_app.py
Browse files- src/streamlit_app.py +113 -17
src/streamlit_app.py
CHANGED
|
@@ -2,9 +2,12 @@ import streamlit as st
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
from langdetect import detect
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
-
# Load Model
|
| 8 |
# ----------------------------
|
| 9 |
MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 10 |
|
|
@@ -20,6 +23,15 @@ def load_model():
|
|
| 20 |
|
| 21 |
tokenizer, model = load_model()
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ----------------------------
|
| 24 |
# Language Detection
|
| 25 |
# ----------------------------
|
|
@@ -30,20 +42,20 @@ def detect_lang(text):
|
|
| 30 |
return "en"
|
| 31 |
|
| 32 |
# ----------------------------
|
| 33 |
-
#
|
| 34 |
# ----------------------------
|
| 35 |
def build_prompt(user_input, lang):
|
| 36 |
if lang == "fr":
|
| 37 |
-
system = "Tu es un tuteur de mathรฉmatiques pour enfants.
|
| 38 |
-
elif lang == "sw":
|
| 39 |
system = "Wewe ni mwalimu wa hesabu kwa watoto. Eleza kwa urahisi."
|
| 40 |
else:
|
| 41 |
-
system = "You are a friendly math tutor for kids. Explain
|
| 42 |
|
| 43 |
return f"{system}\nUser: {user_input}\nAssistant:"
|
| 44 |
|
| 45 |
# ----------------------------
|
| 46 |
-
# Generate Response
|
| 47 |
# ----------------------------
|
| 48 |
def generate(prompt):
|
| 49 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
@@ -56,19 +68,103 @@ def generate(prompt):
|
|
| 56 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 57 |
|
| 58 |
# ----------------------------
|
| 59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# ----------------------------
|
| 61 |
-
|
| 62 |
-
st.
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
prompt = build_prompt(user_input, lang)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
st.
|
| 74 |
-
st.write(
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
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
|
| 11 |
# ----------------------------
|
| 12 |
MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 13 |
|
|
|
|
| 23 |
|
| 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
|
| 37 |
# ----------------------------
|
|
|
|
| 42 |
return "en"
|
| 43 |
|
| 44 |
# ----------------------------
|
| 45 |
+
# Multilingual Prompt
|
| 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:
|
| 53 |
+
system = "You are a friendly math tutor for kids. Explain step by step."
|
| 54 |
|
| 55 |
return f"{system}\nUser: {user_input}\nAssistant:"
|
| 56 |
|
| 57 |
# ----------------------------
|
| 58 |
+
# Generate LLM Response
|
| 59 |
# ----------------------------
|
| 60 |
def generate(prompt):
|
| 61 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
| 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"))
|
| 75 |
+
binary = img > 128
|
| 76 |
+
count = int(binary.sum() / 500)
|
| 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 |
+
|
| 98 |
+
if user_input:
|
| 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 |
+
|
| 116 |
+
if uploaded:
|
| 117 |
+
image = Image.open(uploaded)
|
| 118 |
+
st.image(image, caption="Uploaded Image")
|
| 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 โ")
|