Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,17 +6,56 @@ from bs4 import BeautifulSoup
|
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# --- Search online content ---
|
| 10 |
def search_online_syllabus(grade, subject):
|
| 11 |
urls = {
|
| 12 |
-
"5": {
|
| 13 |
"Science": [
|
| 14 |
-
"https://www.pctb.punjab.gov.pk/books",
|
| 15 |
"https://www.cambridgeinternational.org/programmes-and-qualifications/cambridge-primary/curriculum/science/",
|
| 16 |
"https://global.oup.com/education/content/primary/series/oxford-international-primary-science/"
|
| 17 |
]
|
| 18 |
},
|
| 19 |
-
"6": {
|
| 20 |
"Science": [
|
| 21 |
"https://www.pctb.punjab.gov.pk/books",
|
| 22 |
"https://www.cambridgeinternational.org/programmes-and-qualifications/cambridge-primary/curriculum/science/",
|
|
@@ -36,15 +75,15 @@ def search_online_syllabus(grade, subject):
|
|
| 36 |
combined_text += f"[Error fetching {url}]: {str(e)}\n"
|
| 37 |
return combined_text
|
| 38 |
|
| 39 |
-
# --- Answer
|
| 40 |
def get_answer(user_question, syllabus_text):
|
| 41 |
chunks = syllabus_text.split('\n\n')
|
| 42 |
vectorizer = TfidfVectorizer().fit_transform([user_question] + chunks)
|
| 43 |
cosine_similarities = cosine_similarity(vectorizer[0:1], vectorizer[1:]).flatten()
|
| 44 |
best_chunk = chunks[cosine_similarities.argmax()]
|
| 45 |
|
| 46 |
-
beginner = f"سیدھا سا جواب: {best_chunk.strip()}"
|
| 47 |
-
story = f"
|
| 48 |
return beginner, story
|
| 49 |
|
| 50 |
# --- Urdu Text-to-Speech ---
|
|
@@ -54,26 +93,23 @@ def text_to_speech_urdu(text):
|
|
| 54 |
tts.save(tmp_file.name)
|
| 55 |
return tmp_file.name
|
| 56 |
|
| 57 |
-
# ---
|
| 58 |
-
st.
|
| 59 |
-
st.
|
| 60 |
-
st.markdown("<p style='text-align: center;'>Grade 5 & 6 Kids | Learn Science with Stories</p>", unsafe_allow_html=True)
|
| 61 |
-
|
| 62 |
-
grade = st.selectbox("🎓 Select Grade:", ["5", "6"])
|
| 63 |
-
subject = st.selectbox("📚 Select Subject:", ["Science"])
|
| 64 |
-
question = st.text_area("❓ Ask your question (Urdu/English):")
|
| 65 |
|
|
|
|
| 66 |
if st.button("🔍 Get Answer"):
|
| 67 |
-
with st.spinner("📡 Searching online
|
| 68 |
syllabus_text = search_online_syllabus(grade, subject)
|
|
|
|
| 69 |
beginner, story = get_answer(question, syllabus_text)
|
| 70 |
|
| 71 |
st.subheader("💡 Beginner-Friendly Answer:")
|
| 72 |
-
st.
|
| 73 |
|
| 74 |
-
st.subheader("📖 Storytelling Style:")
|
| 75 |
-
st.
|
| 76 |
|
| 77 |
-
if st.button("🔈 Hear
|
| 78 |
audio_path = text_to_speech_urdu(beginner + "۔ " + story)
|
| 79 |
st.audio(audio_path, format='audio/mp3')
|
|
|
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
|
| 9 |
+
# --- Set Page Config ---
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Education with Fun",
|
| 12 |
+
layout="centered",
|
| 13 |
+
page_icon="📘",
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# --- Background Image ---
|
| 17 |
+
def set_bg():
|
| 18 |
+
st.markdown(
|
| 19 |
+
f"""
|
| 20 |
+
<style>
|
| 21 |
+
.stApp {{
|
| 22 |
+
background-image: url("https://cdn.pixabay.com/photo/2017/06/29/02/05/classroom-2454153_1280.jpg");
|
| 23 |
+
background-size: cover;
|
| 24 |
+
background-repeat: no-repeat;
|
| 25 |
+
background-attachment: fixed;
|
| 26 |
+
}}
|
| 27 |
+
</style>
|
| 28 |
+
""",
|
| 29 |
+
unsafe_allow_html=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
set_bg()
|
| 33 |
+
|
| 34 |
+
# --- Header Banner ---
|
| 35 |
+
st.markdown(
|
| 36 |
+
"""
|
| 37 |
+
<div style='text-align: center; padding: 20px; background-color: rgba(255, 255, 255, 0.8); border-radius: 15px;'>
|
| 38 |
+
<h1 style='color: green;'>📘 Education with Fun</h1>
|
| 39 |
+
<h4>Learn Science with Stories and Audio (Grade 5 & 6)</h4>
|
| 40 |
+
</div>
|
| 41 |
+
""", unsafe_allow_html=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# --- Grade & Subject Selection ---
|
| 45 |
+
grade = st.selectbox("🎓 Select Grade:", ["Grade 5", "Grade 6"])
|
| 46 |
+
subject = st.selectbox("📚 Select Subject:", ["Science"])
|
| 47 |
+
|
| 48 |
# --- Search online content ---
|
| 49 |
def search_online_syllabus(grade, subject):
|
| 50 |
urls = {
|
| 51 |
+
"Grade 5": {
|
| 52 |
"Science": [
|
| 53 |
+
"https://www.pctb.punjab.gov.pk/books",
|
| 54 |
"https://www.cambridgeinternational.org/programmes-and-qualifications/cambridge-primary/curriculum/science/",
|
| 55 |
"https://global.oup.com/education/content/primary/series/oxford-international-primary-science/"
|
| 56 |
]
|
| 57 |
},
|
| 58 |
+
"Grade 6": {
|
| 59 |
"Science": [
|
| 60 |
"https://www.pctb.punjab.gov.pk/books",
|
| 61 |
"https://www.cambridgeinternational.org/programmes-and-qualifications/cambridge-primary/curriculum/science/",
|
|
|
|
| 75 |
combined_text += f"[Error fetching {url}]: {str(e)}\n"
|
| 76 |
return combined_text
|
| 77 |
|
| 78 |
+
# --- Answer Generation ---
|
| 79 |
def get_answer(user_question, syllabus_text):
|
| 80 |
chunks = syllabus_text.split('\n\n')
|
| 81 |
vectorizer = TfidfVectorizer().fit_transform([user_question] + chunks)
|
| 82 |
cosine_similarities = cosine_similarity(vectorizer[0:1], vectorizer[1:]).flatten()
|
| 83 |
best_chunk = chunks[cosine_similarities.argmax()]
|
| 84 |
|
| 85 |
+
beginner = f"📗 سیدھا سا جواب: {best_chunk.strip()}"
|
| 86 |
+
story = f"📙 کہانی کی صورت میں: فرض کریں آپ ایک چھوٹے سائنسدان ہیں... {best_chunk.strip()}"
|
| 87 |
return beginner, story
|
| 88 |
|
| 89 |
# --- Urdu Text-to-Speech ---
|
|
|
|
| 93 |
tts.save(tmp_file.name)
|
| 94 |
return tmp_file.name
|
| 95 |
|
| 96 |
+
# --- Question Input ---
|
| 97 |
+
st.markdown("### ❓ Ask your question (Urdu or English):")
|
| 98 |
+
question = st.text_area("", placeholder="e.g., What is evaporation? / بخارات کیا ہوتے ہیں؟")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# --- Submit Button ---
|
| 101 |
if st.button("🔍 Get Answer"):
|
| 102 |
+
with st.spinner("📡 Searching online syllabus..."):
|
| 103 |
syllabus_text = search_online_syllabus(grade, subject)
|
| 104 |
+
|
| 105 |
beginner, story = get_answer(question, syllabus_text)
|
| 106 |
|
| 107 |
st.subheader("💡 Beginner-Friendly Answer:")
|
| 108 |
+
st.success(beginner)
|
| 109 |
|
| 110 |
+
st.subheader("📖 Storytelling Style Answer:")
|
| 111 |
+
st.info(story)
|
| 112 |
|
| 113 |
+
if st.button("🔈 Hear Urdu Audio"):
|
| 114 |
audio_path = text_to_speech_urdu(beginner + "۔ " + story)
|
| 115 |
st.audio(audio_path, format='audio/mp3')
|