Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
|
|
3 |
from datetime import datetime
|
4 |
import base64
|
5 |
import os
|
|
|
|
|
6 |
|
7 |
# Initialize models
|
8 |
summarizer = pipeline("summarization")
|
@@ -10,21 +13,20 @@ translator_en_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
|
|
10 |
translator_ur_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
|
11 |
grammar_corrector = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
12 |
|
13 |
-
# Set up page config
|
14 |
st.set_page_config(page_title="CSS AI Assistant by Komal", layout="wide")
|
15 |
|
16 |
st.title("π CSS AI Assistant π΅π°")
|
17 |
st.write("Helping students with news summaries, precis, essays & more β for free!")
|
18 |
|
19 |
-
# Sidebar Menu
|
20 |
menu = st.sidebar.radio("Select Feature", [
|
21 |
"Daily News Summary",
|
22 |
"Precis Evaluation",
|
23 |
"Essay Feedback",
|
24 |
-
"Saved Notes"
|
|
|
25 |
])
|
26 |
|
27 |
-
#
|
28 |
NOTES_FILE = "saved_notes.txt"
|
29 |
|
30 |
def save_note(title, content):
|
@@ -41,14 +43,28 @@ def generate_download_link(text, filename):
|
|
41 |
b64 = base64.b64encode(text.encode()).decode()
|
42 |
return f'<a href="data:file/txt;base64,{b64}" download="{filename}">π₯ Download Notes</a>'
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# --- Daily News Summary ---
|
45 |
if menu == "Daily News Summary":
|
46 |
st.header("π° AI-Powered News Summarizer")
|
47 |
-
user_news = st.text_area("Paste or write today's news:", height=
|
48 |
lang = st.radio("Language", ["English", "Urdu"])
|
49 |
|
50 |
if st.button("Summarize") and user_news:
|
51 |
-
summary = summarizer(user_news, max_length=
|
52 |
if lang == "Urdu":
|
53 |
translated = translator_en_ur(summary)[0]['translation_text']
|
54 |
st.success("Summary in Urdu:")
|
@@ -64,7 +80,7 @@ if menu == "Daily News Summary":
|
|
64 |
# --- Precis Evaluation ---
|
65 |
elif menu == "Precis Evaluation":
|
66 |
st.header("βοΈ Precis Evaluation Tool")
|
67 |
-
precis_input = st.text_area("Enter your precis for evaluation:", height=
|
68 |
if st.button("Evaluate Precis") and precis_input:
|
69 |
corrected = grammar_corrector("grammar: " + precis_input)[0]['generated_text']
|
70 |
st.success("β
Corrected Precis:")
|
@@ -75,7 +91,7 @@ elif menu == "Precis Evaluation":
|
|
75 |
# --- Essay Feedback ---
|
76 |
elif menu == "Essay Feedback":
|
77 |
st.header("ποΈ Essay Evaluation Tool")
|
78 |
-
essay_input = st.text_area("Paste your essay here:", height=
|
79 |
if st.button("Evaluate Essay") and essay_input:
|
80 |
corrected = grammar_corrector("grammar: " + essay_input)[0]['generated_text']
|
81 |
st.success("β
Suggestions:")
|
@@ -89,3 +105,29 @@ elif menu == "Saved Notes":
|
|
89 |
notes = load_notes()
|
90 |
st.markdown(notes)
|
91 |
st.markdown(generate_download_link(notes, "css_notes.txt"), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
+
from langdetect import detect
|
4 |
from datetime import datetime
|
5 |
import base64
|
6 |
import os
|
7 |
+
import PyPDF2
|
8 |
+
from docx import Document
|
9 |
|
10 |
# Initialize models
|
11 |
summarizer = pipeline("summarization")
|
|
|
13 |
translator_ur_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
|
14 |
grammar_corrector = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
15 |
|
|
|
16 |
st.set_page_config(page_title="CSS AI Assistant by Komal", layout="wide")
|
17 |
|
18 |
st.title("π CSS AI Assistant π΅π°")
|
19 |
st.write("Helping students with news summaries, precis, essays & more β for free!")
|
20 |
|
|
|
21 |
menu = st.sidebar.radio("Select Feature", [
|
22 |
"Daily News Summary",
|
23 |
"Precis Evaluation",
|
24 |
"Essay Feedback",
|
25 |
+
"Saved Notes",
|
26 |
+
"File Upload"
|
27 |
])
|
28 |
|
29 |
+
# Local file to save notes
|
30 |
NOTES_FILE = "saved_notes.txt"
|
31 |
|
32 |
def save_note(title, content):
|
|
|
43 |
b64 = base64.b64encode(text.encode()).decode()
|
44 |
return f'<a href="data:file/txt;base64,{b64}" download="{filename}">π₯ Download Notes</a>'
|
45 |
|
46 |
+
def extract_text_from_pdf(uploaded_file):
|
47 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
48 |
+
text = ""
|
49 |
+
for page in pdf_reader.pages:
|
50 |
+
text += page.extract_text()
|
51 |
+
return text
|
52 |
+
|
53 |
+
def extract_text_from_word(uploaded_file):
|
54 |
+
doc = Document(uploaded_file)
|
55 |
+
text = ""
|
56 |
+
for para in doc.paragraphs:
|
57 |
+
text += para.text
|
58 |
+
return text
|
59 |
+
|
60 |
# --- Daily News Summary ---
|
61 |
if menu == "Daily News Summary":
|
62 |
st.header("π° AI-Powered News Summarizer")
|
63 |
+
user_news = st.text_area("Paste or write today's news:", height=300)
|
64 |
lang = st.radio("Language", ["English", "Urdu"])
|
65 |
|
66 |
if st.button("Summarize") and user_news:
|
67 |
+
summary = summarizer(user_news, max_length=500, min_length=100, do_sample=False)[0]['summary_text']
|
68 |
if lang == "Urdu":
|
69 |
translated = translator_en_ur(summary)[0]['translation_text']
|
70 |
st.success("Summary in Urdu:")
|
|
|
80 |
# --- Precis Evaluation ---
|
81 |
elif menu == "Precis Evaluation":
|
82 |
st.header("βοΈ Precis Evaluation Tool")
|
83 |
+
precis_input = st.text_area("Enter your precis for evaluation:", height=300)
|
84 |
if st.button("Evaluate Precis") and precis_input:
|
85 |
corrected = grammar_corrector("grammar: " + precis_input)[0]['generated_text']
|
86 |
st.success("β
Corrected Precis:")
|
|
|
91 |
# --- Essay Feedback ---
|
92 |
elif menu == "Essay Feedback":
|
93 |
st.header("ποΈ Essay Evaluation Tool")
|
94 |
+
essay_input = st.text_area("Paste your essay here:", height=400)
|
95 |
if st.button("Evaluate Essay") and essay_input:
|
96 |
corrected = grammar_corrector("grammar: " + essay_input)[0]['generated_text']
|
97 |
st.success("β
Suggestions:")
|
|
|
105 |
notes = load_notes()
|
106 |
st.markdown(notes)
|
107 |
st.markdown(generate_download_link(notes, "css_notes.txt"), unsafe_allow_html=True)
|
108 |
+
|
109 |
+
# --- File Upload ---
|
110 |
+
elif menu == "File Upload":
|
111 |
+
st.header("π File Upload - PDF or Word")
|
112 |
+
uploaded_file = st.file_uploader("Upload a PDF or Word file", type=["pdf", "docx"])
|
113 |
+
|
114 |
+
if uploaded_file is not None:
|
115 |
+
file_type = uploaded_file.type
|
116 |
+
if file_type == "application/pdf":
|
117 |
+
file_text = extract_text_from_pdf(uploaded_file)
|
118 |
+
elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
119 |
+
file_text = extract_text_from_word(uploaded_file)
|
120 |
+
|
121 |
+
st.text_area("Extracted Text", file_text, height=300)
|
122 |
+
|
123 |
+
# Now you can apply the summarization, translation, or grammar correction on the extracted text
|
124 |
+
lang = st.radio("Language for Summary/Translation", ["English", "Urdu"])
|
125 |
+
if st.button("Summarize and Translate") and file_text:
|
126 |
+
summary = summarizer(file_text, max_length=500, min_length=100, do_sample=False)[0]['summary_text']
|
127 |
+
if lang == "Urdu":
|
128 |
+
translated = translator_en_ur(summary)[0]['translation_text']
|
129 |
+
st.success("Summary in Urdu:")
|
130 |
+
st.write(translated)
|
131 |
+
else:
|
132 |
+
st.success("Summary in English:")
|
133 |
+
st.write(summary)
|