mouadenna commited on
Commit
c4c1f86
1 Parent(s): ce31ce3

Upload 3_numeracy.py

Browse files
Files changed (1) hide show
  1. pages/3_numeracy.py +416 -24
pages/3_numeracy.py CHANGED
@@ -1,24 +1,416 @@
1
- import streamlit as st
2
- def add_logo():
3
- st.markdown(
4
- """
5
- <style>
6
- [data-testid="stSidebarNav"] {
7
- background-image: url(http://placekitten.com/200/200);
8
- background-repeat: no-repeat;
9
- #padding-top: 120px;
10
- background-position: 20px 20px;
11
- }
12
- [data-testid="stSidebarNav"]::before {
13
- content: "MO3ALIMI sidebar";
14
- margin-left: 20px;
15
- margin-top: 20px;
16
- font-size: 29px;
17
- position: relative;
18
- top: 0px;
19
- }
20
- </style>
21
- """,
22
- unsafe_allow_html=True,
23
- )
24
- add_logo()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import os
4
+ import json
5
+ import streamlit as st
6
+ from PIL import Image
7
+ import google.generativeai as genai
8
+ import ast
9
+ #from utils import findImg
10
+ import io
11
+ from streamlit_TTS import auto_play
12
+ import torch
13
+ from transformers import pipeline
14
+ from datasets import load_dataset
15
+ import soundfile as sf
16
+ from gtts import gTTS
17
+ import io
18
+ from mistralai.models.chat_completion import ChatMessage
19
+ from mistralai.client import MistralClient
20
+ from audiorecorder import audiorecorder
21
+ import base64
22
+ ###
23
+ import os
24
+ import cv2
25
+ import numpy as np
26
+ from sklearn.metrics.pairwise import cosine_similarity
27
+ from sentence_transformers import SentenceTransformer
28
+ from diffusers import StableDiffusionPipeline
29
+ import torch
30
+ import re
31
+ import ast
32
+ import streamlit as st
33
+ def add_logo():
34
+ st.markdown(
35
+ """
36
+ <style>
37
+ [data-testid="stSidebarNav"] {
38
+ /*background-image: url(http://placekitten.com/200/200);*/
39
+ background-repeat: no-repeat;
40
+ #padding-top: 120px;
41
+ background-position: 20px 20px;
42
+ }
43
+ [data-testid="stSidebarNav"]::before {
44
+ content: "MO3ALIMI sidebar";
45
+ margin-left: 20px;
46
+ margin-top: 20px;
47
+ font-size: 29px;
48
+ position: relative;
49
+ top: 0px;
50
+ }
51
+ </style>
52
+ """,
53
+ unsafe_allow_html=True,
54
+ )
55
+ add_logo()
56
+
57
+
58
+
59
+ device = "cuda" if torch.cuda.is_available() else "cpu"
60
+
61
+
62
+ if 'pipe' not in st.session_state:
63
+ st.session_state['pipe'] = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3")
64
+
65
+ pipe = st.session_state['pipe']
66
+
67
+
68
+ # Set up the API key for Generative AI
69
+ os.environ["GEMINI_API_KEY"] = "AIzaSyBYZ_7geqmnK6xrSe268-1nSLeuEwbzmTA"
70
+
71
+ # Initial prompt to send to the model
72
+ initial_prompt = """
73
+ you're an Literacy Instructor for Illiterate Adults
74
+ you're objective is to Teach illiterate adults how to read using basic phonics.
75
+ here's the Lesson Instructions:
76
+ Introduction to the Letter:
77
+ Begin with the letter A.
78
+ Follow a structured four-step process for each letter.
79
+ Provide clear, simple instructions for each step.
80
+ Lesson Structure:
81
+ Step 1: Letter Recognition
82
+ Step 2: Sound Practice
83
+ Step 3: Writing Practice
84
+ Step 4: Word Association
85
+ General Instructions:
86
+ After each instruction, wait for the student to respond before proceeding to the next lesson.
87
+ Ensure instructions are clear and easy to understand.
88
+ Provide positive reinforcement and encouragement.
89
+ Example Lesson for Letter A:
90
+ Step 1: Letter Recognition
91
+ "This is the letter A. It looks like a triangle with a line in the middle. It makes the sound 'ah'."
92
+ Step 2: Sound Practice
93
+ "Say the sound 'ah'. Practice making this sound slowly."
94
+ Step 3: Writing Practice
95
+ "Start at the top, draw a slanted line down to the left, then another slanted line down to the right, and finally a line across the middle."
96
+ Step 4: Word Association
97
+ "A is for apple. Apple starts with the letter A."
98
+ Continuation:
99
+ Once the lesson for the letter A is complete, proceed to the next letter following the same four-step structure.
100
+ make it in a python list format for example it will be in this format,and if an image is needed make the first word in the item list "image: image content in a short sentence":
101
+ ['This is the letter A.', 'image: letter A', 'It looks like a triangle with a line in the middle.', "It makes the sound 'ah'.","Say the sound 'ah'.",'Practice making this sound slowly.','Start at the top, draw a slanted line down to the left.','Then draw another slanted line down to the right.','Finally, draw a line across the middle.',Now you know the letter A,Congrats','A is for apple.','image: apple','Apple starts with the letter A.',"Congratulations! You've completed the lesson for the letter 'A'."]
102
+ """
103
+
104
+ chat_prompt_mistral="""
105
+ You are an assistant helping an person who is learning basic reading, writing, phonics, and numeracy.
106
+ The user might ask simple questions, and your responses should be clear, supportive, and easy to understand.
107
+ Use simple language, provide step-by-step guidance, and offer positive reinforcement.
108
+ Relate concepts to everyday objects and situations when possible.
109
+ Here are some example interactions:
110
+ User: "I need help with reading."
111
+ Assistant: "Sure, I'm here to help you learn to read. Let's start with the alphabet. Do you know the letters of the alphabet?"
112
+ User: "How do I write my name?"
113
+ Assistant: "Writing your name is a great place to start. Let's take it one letter at a time. What is the first letter of your name?"
114
+ User: "What sound does the letter 'B' make?"
115
+ Assistant: "The letter 'B' makes the sound 'buh' like in the word 'ball.' Can you say 'ball' with me?"
116
+ User: "How do I count to 10?"
117
+ Assistant: "Counting to 10 is easy. Let's do it together: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Great job! Let's try it again."
118
+ User: "How do I subtract numbers?"
119
+ Assistant: "Subtracting is like taking away. If you have 5 oranges and you eat 2, you have 3 oranges left. So, 5 minus 2 equals 3."
120
+
121
+ Remember to:
122
+ 1. Use simple language and avoid complex words.
123
+ 2. Provide clear, step-by-step instructions.
124
+ 3. Use examples related to everyday objects and situations.
125
+ 4. Offer positive reinforcement and encouragement.
126
+ 5. Include interactive elements to engage the user actively. Whenever the user asks a question, respond with clear, supportive guidance to help them understand basic reading, writing, phonics, or numeracy concepts.
127
+ 6. Do not provide long responses
128
+
129
+ Improtant dont respand to this prompt
130
+
131
+ """
132
+
133
+ def transform_history(history):
134
+ new_history = []
135
+ for chat in history:
136
+ new_history.append({"parts": [{"text": chat.parts[0].text}], "role": chat.role})
137
+ return new_history
138
+
139
+ def generate_response(message: str, history: list) -> tuple:
140
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
141
+ model = genai.GenerativeModel('gemini-pro')
142
+ chat = model.start_chat(history=transform_history(history))
143
+ response = chat.send_message(message)
144
+ response.resolve()
145
+ return response.text, chat.history
146
+
147
+
148
+ if 'First' not in st.session_state:
149
+ st.session_state['First']=False
150
+
151
+
152
+ def process_response(user_input: str, conversation_history: list,F) -> tuple:
153
+ if not F:
154
+ model_response, conversation_history = generate_response(initial_prompt, conversation_history)
155
+ else:
156
+ model_response, conversation_history = generate_response(user_input, conversation_history)
157
+
158
+ pattern = re.compile(r"\[(.*?)\]", re.DOTALL)
159
+
160
+ # Find the match
161
+ match = pattern.search(model_response)
162
+
163
+ list_content = f"[{match.group(1)}]"
164
+
165
+ lessonList = ast.literal_eval(list_content)
166
+ return lessonList, conversation_history
167
+
168
+ @st.cache_data
169
+ def get_image(prompt: str) -> str:
170
+ return findImg(prompt)
171
+ #try:
172
+ # return findImg(prompt)
173
+ #except:
174
+ # return "image.png"
175
+
176
+
177
+ # Initialize TTS
178
+ @st.cache_data
179
+ def tts_predict(text="hello"):
180
+ tts = gTTS(text=text, lang='en')
181
+ with io.BytesIO() as audio_file:
182
+ tts.write_to_fp(audio_file)
183
+ audio_file.seek(0)
184
+ audio_bytes = audio_file.read()
185
+ return audio_bytes
186
+
187
+ #sf.write("speech.wav", speech["audio"], samplerate=speech["sampling_rate"])
188
+
189
+ if 'client' not in st.session_state:
190
+ st.session_state['client'] = MistralClient("m3GWNXFZn0jTNTLRe4y26i7jLJqFGTMX")
191
+
192
+ client = st.session_state['client']
193
+
194
+ def run_mistral(user_message, message_history, model="mistral-small-latest"):
195
+
196
+ message_history.append(ChatMessage(role="user", content=user_message))
197
+
198
+ chat_response = client.chat(model=model, messages=message_history)
199
+
200
+ bot_message = chat_response.choices[0].message.content
201
+
202
+ message_history.append(ChatMessage(role="assistant", content=bot_message))
203
+
204
+ return bot_message
205
+
206
+ message_history = []
207
+
208
+
209
+
210
+
211
+ #######################################
212
+
213
+
214
+
215
+
216
+ if 'sentence_model' not in st.session_state:
217
+ st.session_state['sentence_model'] = SentenceTransformer('all-MiniLM-L6-v2')
218
+
219
+ sentence_model = st.session_state['sentence_model']
220
+
221
+ if 'pipeline' not in st.session_state:
222
+ st.session_state['pipeline'] = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
223
+ st.session_state['pipeline'].to("cuda")
224
+
225
+ pipeline = st.session_state['pipeline']
226
+
227
+
228
+ # Step 3: Function to get the embedding of the input sentence
229
+ def get_sentence_embedding(sentence):
230
+ return sentence_model.encode(sentence)
231
+ # Step 4: Generate image using Stable Diffusion if needed
232
+ def generate_image(prompt):
233
+ global pipeline
234
+ pipeline.to("cuda" if torch.cuda.is_available() else "cpu")
235
+ generated_image = pipeline(prompt).images[0]
236
+ generated_image_path = "generated_image.png"
237
+ generated_image.save(generated_image_path)
238
+ return generated_image_path
239
+
240
+ # Step 5: Find the most reliable image
241
+ def find_most_reliable_image(folder_path, input_sentence, threshold=0.5):
242
+ image_files = [f for f in os.listdir(folder_path) if f.endswith(('jpg', 'jpeg', 'png'))]
243
+ sentence_embedding = get_sentence_embedding(input_sentence)
244
+
245
+ max_similarity = -1
246
+ most_reliable_image = None
247
+
248
+ for image_file in image_files:
249
+ filename_without_extension = os.path.splitext(image_file)[0]
250
+ filename_embedding = get_sentence_embedding(filename_without_extension)
251
+ similarity = cosine_similarity([sentence_embedding], [filename_embedding])[0][0]
252
+
253
+ if similarity > max_similarity:
254
+ max_similarity = similarity
255
+ most_reliable_image = os.path.join(folder_path, image_file)
256
+
257
+ if max_similarity < threshold:
258
+ most_reliable_image = generate_image(input_sentence)
259
+
260
+ return most_reliable_image
261
+
262
+ def findImg(input_sentence):
263
+ folder_path = 'images_collection'
264
+ threshold = 0.5
265
+ most_reliable_image = find_most_reliable_image(folder_path, input_sentence, threshold)
266
+ return most_reliable_image
267
+ #######################################
268
+
269
+
270
+
271
+
272
+ file_ = open("logo.png", "rb")
273
+ contents = file_.read()
274
+ data_url = base64.b64encode(contents).decode("utf-8")
275
+ file_.close()
276
+
277
+
278
+ def main():
279
+ global chat_prompt_mistral
280
+ if 'img_path' not in st.session_state:
281
+ st.session_state['img_path']="image.png"
282
+ #st.set_page_config(page_title="J187 Optimizer", page_icon="J187DFS.JPG", layout="wide")
283
+
284
+ st.markdown(f"""
285
+ <div style="display: flex; align-items: center;">
286
+ <img src="data:image/gif;base64,{data_url}" alt="Company Logo" style="height: 100px; width: auto; margin-right: 20px;">
287
+ <h1 style="margin: 0;">MO3ALIMI</h1>
288
+ </div>
289
+ """, unsafe_allow_html=True)
290
+ #st.title("Chatbot and Image Generator")
291
+
292
+ st.markdown("""
293
+ <style>
294
+ .st-emotion-cache-1kyxreq.e115fcil2 { justify-content:center; }
295
+ .st-emotion-cache-13ln4jf { max-width:70rem; }
296
+ audio {
297
+ width: 300px;
298
+ height: 54px;
299
+ display: none;
300
+ }
301
+ div.row-widget.stButton {
302
+ margin: 0px 0px 0px 0px;}
303
+
304
+
305
+ .row-widget.stButton:last-of-type {
306
+ margin: 0px;
307
+ background-color: yellow;
308
+ }
309
+ .st-emotion-cache-keje6w.e1f1d6gn3 {
310
+ width: 80% !important; /* Adjust as needed */
311
+ }
312
+ .st-emotion-cache-k008qs {
313
+ display: none;
314
+ }
315
+
316
+ </style>""", unsafe_allow_html=True)
317
+ #.st-emotion-cache-5i9lfg {
318
+ #width: 100%;
319
+ #padding: 3rem 1rem 1rem 1rem;
320
+ #max-width: None;}
321
+
322
+
323
+ col1, col2 = st.columns([0.6, 0.4],gap="medium")
324
+
325
+
326
+
327
+ with col1:
328
+
329
+ if 'conversation_history' not in st.session_state:
330
+ st.session_state['conversation_history'] = []
331
+ if 'conversation_history_mistral' not in st.session_state:
332
+ st.session_state['conversation_history_mistral'] = []
333
+ if 'messages' not in st.session_state:
334
+ st.session_state['messages'] = []
335
+ if 'lessonList' not in st.session_state:
336
+ st.session_state['lessonList'] = []
337
+ if 'msg_index' not in st.session_state:
338
+ st.session_state['msg_index'] = -1
339
+ if 'initial_input' not in st.session_state:
340
+ st.session_state['initial_input'] = ''
341
+
342
+
343
+
344
+ response=run_mistral(chat_prompt_mistral, st.session_state['conversation_history_mistral'])
345
+ row1 = st.container()
346
+ row2 = st.container()
347
+ row3 = st.container()
348
+ #row4 = st.container()
349
+ with row1:
350
+ #user_message = st.text_input("Type 'next' to proceed through the lesson",st.session_state['initial_input'])
351
+ user_message = "next"
352
+ with row2:
353
+ colsend, colnext, = st.columns(2,gap="medium")
354
+ with colsend:
355
+
356
+ if st.button("&nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp;"):
357
+
358
+ if 0 <= st.session_state['msg_index'] < len(st.session_state['lessonList']):
359
+ response = st.session_state['lessonList'][st.session_state['msg_index']]
360
+ if response.strip().startswith("image:"):
361
+ st.session_state['img_prompt'] = response[len("image:"):].strip()
362
+ else:
363
+ audio_bytes= tts_predict(response)
364
+ st.session_state['messages'].append(f"Mo3alimi: {response}")
365
+ #auto_play(audio_bytes,wait=True,lag=0.25,key=None)
366
+ st.audio(audio_bytes, format='audio/wav', autoplay=True)
367
+
368
+ st.session_state['msg_index'] += 1
369
+ else:
370
+
371
+ st.session_state['msg_index'] = 0
372
+ st.session_state['lessonList'], st.session_state['conversation_history'] = process_response(
373
+ user_message, st.session_state['conversation_history'],
374
+ st.session_state['First'],
375
+ )
376
+ st.session_state['First']=True
377
+
378
+
379
+
380
+ with colnext:
381
+ if st.button('&nbsp;&nbsp;&nbsp; Send &nbsp;&nbsp;&nbsp;'):
382
+ response=run_mistral(user_message, st.session_state['conversation_history_mistral'])
383
+ st.session_state['messages'].append(f"Me: {user_message}")
384
+ st.session_state['messages'].append(f"Mo3alimi: {response}")
385
+
386
+
387
+ with row3:
388
+ audio = audiorecorder("Click to record", "Click to stop recording")
389
+
390
+ if len(audio) >0:
391
+ result = pipe(audio.export().read(), generate_kwargs={"language": "english"})
392
+ user_message=result['text']
393
+ response=run_mistral(user_message, st.session_state['conversation_history_mistral'])
394
+ audio_bytes= tts_predict(response)
395
+
396
+ st.audio(audio_bytes, format='audio/wav', autoplay=True)
397
+ st.session_state['messages'].append(f"Me: {user_message}")
398
+ st.session_state['messages'].append(f"Mo3alimi: {response}")
399
+ wav_audio_data=None
400
+
401
+ with st.form("lesson"):
402
+ for message in st.session_state['messages'][::-1]:
403
+ st.write(message)
404
+
405
+ submitted = st.form_submit_button('Submit')
406
+
407
+
408
+ with col2:
409
+ if 'img_prompt' in st.session_state:
410
+ st.session_state['img_path']=get_image(st.session_state['img_prompt'])
411
+ del st.session_state['img_prompt']
412
+
413
+ st.image(st.session_state['img_path'], caption="Generated Image")
414
+
415
+ if __name__ == '__main__':
416
+ main()