Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Set app title and layout
|
| 5 |
+
st.set_page_config(page_title="Advanced Study Helper", page_icon="π", layout="centered")
|
| 6 |
+
st.title("π Advanced Study Helper")
|
| 7 |
+
st.write("Use AI to understand topics, summarize text, or translate from English to Urdu.")
|
| 8 |
+
|
| 9 |
+
# Load models (cached for performance)
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_models():
|
| 12 |
+
study_helper = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 13 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 14 |
+
translator = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
|
| 15 |
+
return study_helper, summarizer, translator
|
| 16 |
+
|
| 17 |
+
study_helper, summarizer, translator = load_models()
|
| 18 |
+
|
| 19 |
+
# --- Tabs for features ---
|
| 20 |
+
tab1, tab2, tab3 = st.tabs(["π Study Help", "π Summarize", "π Translate"])
|
| 21 |
+
|
| 22 |
+
# --- Tab 1: Study Question AI ---
|
| 23 |
+
with tab1:
|
| 24 |
+
st.subheader("Ask a Question or Topic")
|
| 25 |
+
question = st.text_area("βοΈ Type your study question:", height=150, placeholder="e.g., What is the water cycle?")
|
| 26 |
+
if st.button("Explain", key="btn1"):
|
| 27 |
+
if question.strip():
|
| 28 |
+
with st.spinner("Thinking..."):
|
| 29 |
+
output = study_helper(question, max_length=256)[0]['generated_text']
|
| 30 |
+
st.success("β
Explanation:")
|
| 31 |
+
st.write(output)
|
| 32 |
+
else:
|
| 33 |
+
st.warning("Please enter a valid question.")
|
| 34 |
+
|
| 35 |
+
# --- Tab 2: Summarizer ---
|
| 36 |
+
with tab2:
|
| 37 |
+
st.subheader("Summarize a Paragraph")
|
| 38 |
+
text_to_summarize = st.text_area("π Paste a long paragraph:", height=200)
|
| 39 |
+
if st.button("Summarize", key="btn2"):
|
| 40 |
+
if text_to_summarize.strip():
|
| 41 |
+
with st.spinner("Summarizing..."):
|
| 42 |
+
summary = summarizer(text_to_summarize, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
| 43 |
+
st.success("β
Summary:")
|
| 44 |
+
st.write(summary)
|
| 45 |
+
else:
|
| 46 |
+
st.warning("Please paste some text to summarize.")
|
| 47 |
+
|
| 48 |
+
# --- Tab 3: English β Urdu ---
|
| 49 |
+
with tab3:
|
| 50 |
+
st.subheader("Translate English Text to Urdu")
|
| 51 |
+
english_text = st.text_area("π Enter English text to translate:", height=150)
|
| 52 |
+
if st.button("Translate", key="btn3"):
|
| 53 |
+
if english_text.strip():
|
| 54 |
+
with st.spinner("Translating..."):
|
| 55 |
+
urdu_output = translator(english_text)[0]['translation_text']
|
| 56 |
+
st.success("β
Urdu Translation:")
|
| 57 |
+
st.write(urdu_output)
|
| 58 |
+
else:
|
| 59 |
+
st.warning("Please enter English text.")
|