MiakOnline commited on
Commit
dee83bf
·
verified ·
1 Parent(s): 233cfa4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import streamlit as st
4
+ from gtts import gTTS
5
+ import os
6
+ from langdetect import detect
7
+ from sklearn.feature_extraction.text import TfidfVectorizer
8
+ from sklearn.metrics.pairwise import cosine_similarity
9
+ import tempfile
10
+
11
+ # Load syllabus content
12
+ def load_syllabus(grade, subject):
13
+ file_path = f"syllabus/Grade{grade}_{subject}.txt"
14
+ try:
15
+ with open(file_path, 'r', encoding='utf-8') as file:
16
+ return file.read()
17
+ except:
18
+ return "Content not found."
19
+
20
+ # Generate answer using keyword match
21
+ def get_answer(user_question, syllabus_text):
22
+ chunks = syllabus_text.split('\n\n') # Paragraph-wise chunks
23
+ vectorizer = TfidfVectorizer().fit_transform([user_question] + chunks)
24
+ cosine_similarities = cosine_similarity(vectorizer[0:1], vectorizer[1:]).flatten()
25
+ best_chunk = chunks[cosine_similarities.argmax()]
26
+
27
+ beginner = f"سیدھا سا جواب: {best_chunk}"
28
+ story = f"ایک کہانی کی صورت میں جواب: فرض کریں آپ ایک چھوٹے سائنسدان ہیں... {best_chunk}"
29
+ return beginner, story
30
+
31
+ # Urdu Text-to-Speech
32
+ def text_to_speech_urdu(text):
33
+ tts = gTTS(text=text, lang='ur')
34
+ tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
35
+ tts.save(tmp_file.name)
36
+ return tmp_file.name
37
+
38
+ # Streamlit UI
39
+ st.set_page_config(page_title="Education with Fun", layout="centered")
40
+
41
+ st.markdown("<h1 style='text-align: center; color: green;'>📘 Education with Fun</h1>", unsafe_allow_html=True)
42
+ st.markdown("<p style='text-align: center;'>Grade 5 & 6 Kids | Learn Science with Stories</p>", unsafe_allow_html=True)
43
+
44
+ grade = st.selectbox("🎓 Select Grade:", ["5", "6"])
45
+ subject = st.selectbox("📚 Select Subject:", ["Science"])
46
+ question = st.text_area("❓ Ask your question (Urdu/English):")
47
+
48
+ if st.button("🔍 Get Answer"):
49
+ syllabus_text = load_syllabus(grade, subject)
50
+ beginner, story = get_answer(question, syllabus_text)
51
+
52
+ st.subheader("💡 Beginner-Friendly Answer:")
53
+ st.write(beginner)
54
+
55
+ st.subheader("📖 Storytelling Style:")
56
+ st.write(story)
57
+
58
+ # TTS Buttons
59
+ if st.button("🔈 Hear the Answer"):
60
+ audio_path = text_to_speech_urdu(beginner + ". " + story)
61
+ st.audio(audio_path, format='audio/mp3')