Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from gtts import gTTS | |
| import tempfile | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.metrics.pairwise import cosine_similarity | |
| # --- Set Page Config --- | |
| st.set_page_config( | |
| page_title="Education with Fun", | |
| layout="centered", | |
| page_icon="📘" # Changed to string emoji for compatibility | |
| ) | |
| # --- Background Image --- | |
| def set_bg(): | |
| st.markdown( | |
| f""" | |
| <style> | |
| .stApp {{ | |
| background-image: url("https://cdn.pixabay.com/photo/2017/06/29/02/05/classroom-2454153_1280.jpg"); | |
| background-size: cover; | |
| background-repeat: no-repeat; | |
| background-attachment: fixed; | |
| }} | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| set_bg() | |
| # --- Header Banner --- | |
| st.markdown( | |
| """ | |
| <div style='text-align: center; padding: 20px; background-color: rgba(255, 255, 255, 0.8); border-radius: 15px;'> | |
| <h1 style='color: green;'>📘 Education with Fun</h1> | |
| <h4>Learn Science with Stories and Audio (Grade 5 & 6)</h4> | |
| </div> | |
| """, unsafe_allow_html=True | |
| ) | |
| # --- Grade & Subject Selection --- | |
| grade = st.selectbox("🎓 Select Grade:", ["Grade 5", "Grade 6"]) | |
| subject = st.selectbox("📚 Select Subject:", ["Science"]) | |
| # --- Search online content --- | |
| def search_online_syllabus(grade, subject): | |
| urls = { | |
| "Grade 5": { | |
| "Science": [ | |
| "https://www.pctb.punjab.gov.pk/books", | |
| "https://www.cambridgeinternational.org/programmes-and-qualifications/cambridge-primary/curriculum/science/", | |
| "https://global.oup.com/education/content/primary/series/oxford-international-primary-science/" | |
| ] | |
| }, | |
| "Grade 6": { | |
| "Science": [ | |
| "https://www.pctb.punjab.gov.pk/books", | |
| "https://www.cambridgeinternational.org/programmes-and-qualifications/cambridge-primary/curriculum/science/", | |
| "https://global.oup.com/education/content/primary/series/oxford-international-primary-science/" | |
| ] | |
| } | |
| } | |
| combined_text = "" | |
| for url in urls.get(grade, {}).get(subject, []): | |
| try: | |
| res = requests.get(url, timeout=10) | |
| soup = BeautifulSoup(res.text, 'html.parser') | |
| text = soup.get_text(separator=' ') | |
| combined_text += text + "\n\n" | |
| except Exception as e: | |
| combined_text += f"[Error fetching {url}]: {str(e)}\n" | |
| return combined_text | |
| # --- Answer Generation --- | |
| def get_answer(user_question, syllabus_text): | |
| chunks = [chunk.strip() for chunk in syllabus_text.split('\n\n') if chunk.strip()] | |
| vectorizer = TfidfVectorizer().fit_transform([user_question] + chunks) | |
| cosine_similarities = cosine_similarity(vectorizer[0:1], vectorizer[1:]).flatten() | |
| top_idx = cosine_similarities.argmax() | |
| best_chunk = chunks[top_idx] | |
| beginner = f"📗 سیدھا سا جواب: {best_chunk}" | |
| story = f"📙 کہانی کی صورت میں: ایک دن ایک طالب علم نے پوچھا، '{user_question}'۔ استاد نے جواب دیا: {best_chunk}" | |
| return beginner, story | |
| # --- Urdu Text-to-Speech --- | |
| def text_to_speech_urdu(text): | |
| tts = gTTS(text=text, lang='ur') | |
| tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
| tts.save(tmp_file.name) | |
| return tmp_file.name | |
| # --- Question Input --- | |
| st.markdown("### ❓ Ask your question (Urdu or English):") | |
| question = st.text_area("", placeholder="e.g., What is evaporation? / بخارات کیا ہوتے ہیں؟") | |
| # --- Submit Button --- | |
| if st.button("🔍 Get Answer"): | |
| with st.spinner("📡 Searching online syllabus..."): | |
| syllabus_text = search_online_syllabus(grade, subject) | |
| beginner, story = get_answer(question, syllabus_text) | |
| st.subheader("💡 Beginner-Friendly Answer:") | |
| st.success(beginner) | |
| st.subheader("📖 Storytelling Style Answer:") | |
| st.info(story) | |
| if st.button("🔈 Hear Urdu Audio"): | |
| audio_path = text_to_speech_urdu(beginner + "۔ " + story) | |
| st.audio(audio_path, format='audio/mp3') | |