File size: 2,514 Bytes
7cf17a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline

# Set app title and layout
st.set_page_config(page_title="Advanced Study Helper", page_icon="πŸ“˜", layout="centered")
st.title("πŸ“˜ Advanced Study Helper")
st.write("Use AI to understand topics, summarize text, or translate from English to Urdu.")

# Load models (cached for performance)
@st.cache_resource
def load_models():
    study_helper = pipeline("text2text-generation", model="google/flan-t5-base")
    summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
    translator = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
    return study_helper, summarizer, translator

study_helper, summarizer, translator = load_models()

# --- Tabs for features ---
tab1, tab2, tab3 = st.tabs(["πŸ“˜ Study Help", "πŸ“„ Summarize", "🌍 Translate"])

# --- Tab 1: Study Question AI ---
with tab1:
    st.subheader("Ask a Question or Topic")
    question = st.text_area("✍️ Type your study question:", height=150, placeholder="e.g., What is the water cycle?")
    if st.button("Explain", key="btn1"):
        if question.strip():
            with st.spinner("Thinking..."):
                output = study_helper(question, max_length=256)[0]['generated_text']
                st.success("βœ… Explanation:")
                st.write(output)
        else:
            st.warning("Please enter a valid question.")

# --- Tab 2: Summarizer ---
with tab2:
    st.subheader("Summarize a Paragraph")
    text_to_summarize = st.text_area("πŸ“„ Paste a long paragraph:", height=200)
    if st.button("Summarize", key="btn2"):
        if text_to_summarize.strip():
            with st.spinner("Summarizing..."):
                summary = summarizer(text_to_summarize, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
                st.success("βœ… Summary:")
                st.write(summary)
        else:
            st.warning("Please paste some text to summarize.")

# --- Tab 3: English β†’ Urdu ---
with tab3:
    st.subheader("Translate English Text to Urdu")
    english_text = st.text_area("🌐 Enter English text to translate:", height=150)
    if st.button("Translate", key="btn3"):
        if english_text.strip():
            with st.spinner("Translating..."):
                urdu_output = translator(english_text)[0]['translation_text']
                st.success("βœ… Urdu Translation:")
                st.write(urdu_output)
        else:
            st.warning("Please enter English text.")