Spaces:
Running
Running
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) | |
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.") |