KB-Infinity-Tech commited on
Commit
569b6f9
ยท
verified ยท
1 Parent(s): 593ce63

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 (Hugging Face)
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
- # Tutor Prompt
34
  # ----------------------------
35
  def build_prompt(user_input, lang):
36
  if lang == "fr":
37
- system = "Tu es un tuteur de mathรฉmatiques pour enfants. Explique simplement."
38
- elif lang == "sw": # fallback for region
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 simply."
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
- # Streamlit UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # ----------------------------
61
- st.title("๐Ÿง  AI Math Tutor (Hackathon Demo)")
62
- st.write("Multilingual โ€ข Simple โ€ข CPU Friendly")
63
 
64
- user_input = st.text_input("Ask a math question:")
 
 
 
65
 
66
- if user_input:
67
- lang = detect_lang(user_input)
68
- prompt = build_prompt(user_input, lang)
69
 
70
- with st.spinner("Thinking..."):
71
- response = generate(prompt)
72
 
73
- st.write("### ๐Ÿ“˜ Answer")
74
- st.write(response)
 
 
 
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 โœ”")