File size: 4,190 Bytes
dee83bf
 
6d5137e
 
 
dee83bf
 
 
4d24534
 
 
 
53b80ee
4d24534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53b80ee
4d24534
 
 
 
 
 
53b80ee
 
4d24534
6d5137e
 
 
4d24534
6d5137e
4d24534
6d5137e
 
 
 
4d24534
6d5137e
 
 
 
 
 
 
dee83bf
6d5137e
 
 
 
 
 
 
 
 
 
 
4d24534
dee83bf
e59ea13
dee83bf
 
e59ea13
 
 
53b80ee
 
dee83bf
 
6d5137e
dee83bf
 
 
 
 
 
4d24534
 
 
dee83bf
4d24534
53b80ee
 
6d5137e
4d24534
dee83bf
 
53b80ee
4d24534
dee83bf
53b80ee
4d24534
dee83bf
53b80ee
6d5137e
dee83bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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')