aakashdevdat commited on
Commit
6f3b6ef
Β·
verified Β·
1 Parent(s): cb5ba3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -23
app.py CHANGED
@@ -8,18 +8,17 @@ from transformers import pipeline
8
  st.set_page_config(page_title="AI Reading Assistant", layout="wide")
9
 
10
  st.title("πŸ“š AI Reading Assistant")
11
- st.write("Upload a PDF or paste text. Click a word/sentence to get explanation, meaning, and simplification.")
12
 
13
  # -----------------------------
14
- # LOAD MODELS (FREE HF MODELS)
15
  # -----------------------------
16
  @st.cache_resource
17
- def load_models():
18
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
19
- qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
20
- return summarizer, qa_model
21
 
22
- summarizer, qa_model = load_models()
23
 
24
  # -----------------------------
25
  # FILE UPLOAD
@@ -31,10 +30,11 @@ text_data = ""
31
  if uploaded_file:
32
  reader = PdfReader(uploaded_file)
33
  for page in reader.pages:
34
- text_data += page.extract_text()
 
35
 
36
  # -----------------------------
37
- # TEXT INPUT OPTION
38
  # -----------------------------
39
  text_input = st.text_area("Or paste your text here:")
40
 
@@ -42,42 +42,54 @@ if text_input:
42
  text_data = text_input
43
 
44
  # -----------------------------
45
- # DISPLAY TEXT
46
  # -----------------------------
47
  if text_data:
48
- st.subheader("πŸ“„ Your Text")
49
- st.write(text_data[:2000]) # limit display for performance
 
50
 
51
  # -----------------------------
52
  # SIMPLIFY TEXT
53
  # -----------------------------
54
  if st.button("✨ Simplify Paragraph"):
55
  with st.spinner("Simplifying..."):
56
- summary = summarizer(text_data[:1000], max_length=120, min_length=30, do_sample=False)
57
- st.success(summary[0]['summary_text'])
 
 
 
 
 
 
 
 
 
 
58
 
59
  # -----------------------------
60
  # QUESTION ANSWERING
61
  # -----------------------------
62
- st.subheader("❓ Ask a Question from Text")
63
  question = st.text_input("Enter your question:")
64
 
65
  if question:
66
- with st.spinner("Finding answer..."):
67
- answer = qa_model(question=question, context=text_data[:2000])
68
- st.success(f"Answer: {answer['answer']}")
 
69
 
70
  # -----------------------------
71
- # WORD MEANING (SIMPLE)
72
  # -----------------------------
73
  st.subheader("πŸ” Word Explanation")
74
  word = st.text_input("Enter a difficult word:")
75
 
76
  if word:
77
- explanation_prompt = f"Explain the meaning of the word '{word}' in simple English with an example."
78
  with st.spinner("Explaining..."):
79
- explanation = summarizer(explanation_prompt, max_length=60, min_length=20, do_sample=False)
80
- st.success(explanation[0]['summary_text'])
 
81
 
82
  else:
83
  st.info("Upload a PDF or paste text to begin.")
@@ -86,4 +98,4 @@ else:
86
  # FOOTER
87
  # -----------------------------
88
  st.markdown("---")
89
- st.caption("Built with ❀️ using Streamlit + Hugging Face")
 
8
  st.set_page_config(page_title="AI Reading Assistant", layout="wide")
9
 
10
  st.title("πŸ“š AI Reading Assistant")
11
+ st.write("Upload a PDF or paste text. Get simple explanations, summaries, and answers.")
12
 
13
  # -----------------------------
14
+ # LOAD MODEL (LIGHT + STABLE)
15
  # -----------------------------
16
  @st.cache_resource
17
+ def load_model():
18
+ generator = pipeline("text2text-generation", model="google/flan-t5-base")
19
+ return generator
 
20
 
21
+ generator = load_model()
22
 
23
  # -----------------------------
24
  # FILE UPLOAD
 
30
  if uploaded_file:
31
  reader = PdfReader(uploaded_file)
32
  for page in reader.pages:
33
+ if page.extract_text():
34
+ text_data += page.extract_text()
35
 
36
  # -----------------------------
37
+ # TEXT INPUT
38
  # -----------------------------
39
  text_input = st.text_area("Or paste your text here:")
40
 
 
42
  text_data = text_input
43
 
44
  # -----------------------------
45
+ # MAIN FUNCTIONALITY
46
  # -----------------------------
47
  if text_data:
48
+
49
+ st.subheader("πŸ“„ Your Text Preview")
50
+ st.write(text_data[:1500])
51
 
52
  # -----------------------------
53
  # SIMPLIFY TEXT
54
  # -----------------------------
55
  if st.button("✨ Simplify Paragraph"):
56
  with st.spinner("Simplifying..."):
57
+ prompt = f"Explain this in very simple English:\n{text_data[:500]}"
58
+ response = generator(prompt, max_length=150)
59
+ st.success(response[0]['generated_text'])
60
+
61
+ # -----------------------------
62
+ # SUMMARIZE TEXT
63
+ # -----------------------------
64
+ if st.button("πŸ“ Summarize Text"):
65
+ with st.spinner("Summarizing..."):
66
+ prompt = f"Summarize this text:\n{text_data[:500]}"
67
+ response = generator(prompt, max_length=120)
68
+ st.success(response[0]['generated_text'])
69
 
70
  # -----------------------------
71
  # QUESTION ANSWERING
72
  # -----------------------------
73
+ st.subheader("❓ Ask a Question")
74
  question = st.text_input("Enter your question:")
75
 
76
  if question:
77
+ with st.spinner("Thinking..."):
78
+ prompt = f"Answer the question based on the text below:\n\nText:\n{text_data[:700]}\n\nQuestion:\n{question}"
79
+ response = generator(prompt, max_length=120)
80
+ st.success(response[0]['generated_text'])
81
 
82
  # -----------------------------
83
+ # WORD EXPLANATION
84
  # -----------------------------
85
  st.subheader("πŸ” Word Explanation")
86
  word = st.text_input("Enter a difficult word:")
87
 
88
  if word:
 
89
  with st.spinner("Explaining..."):
90
+ prompt = f"Explain the word '{word}' in simple English and give an example."
91
+ response = generator(prompt, max_length=80)
92
+ st.success(response[0]['generated_text'])
93
 
94
  else:
95
  st.info("Upload a PDF or paste text to begin.")
 
98
  # FOOTER
99
  # -----------------------------
100
  st.markdown("---")
101
+ st.caption("Built with ❀️ using Streamlit + Hugging Face (FLAN-T5)")