Spaces:
Running
Running
File size: 5,160 Bytes
5469939 bf2ddb8 939cd60 bf2ddb8 5469939 939cd60 8474e0e 5469939 bf2ddb8 5469939 bf2ddb8 939cd60 bf2ddb8 5469939 bf2ddb8 5469939 bf2ddb8 5469939 939cd60 5469939 939cd60 5469939 939cd60 5469939 bf2ddb8 5469939 939cd60 bf2ddb8 939cd60 bf2ddb8 |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import streamlit as st
from transformers import pipeline
from langdetect import detect
from datetime import datetime
import base64
import os
import PyPDF2
from docx import Document
# Initialize models
summarizer = pipeline("summarization")
translator_en_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
translator_ur_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
grammar_corrector = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
st.set_page_config(page_title="CSS AI Assistant by Komal", layout="wide")
st.title("π CSS AI Assistant π΅π°")
st.write("Helping students with news summaries, precis, essays & more β for free!")
menu = st.sidebar.radio("Select Feature", [
"Daily News Summary",
"Precis Evaluation",
"Essay Feedback",
"Saved Notes",
"File Upload"
])
# Local file to save notes
NOTES_FILE = "saved_notes.txt"
def save_note(title, content):
with open(NOTES_FILE, "a", encoding='utf-8') as f:
f.write(f"\n## {title}\n{content}\n")
def load_notes():
if os.path.exists(NOTES_FILE):
with open(NOTES_FILE, "r", encoding='utf-8') as f:
return f.read()
return "No saved notes yet."
def generate_download_link(text, filename):
b64 = base64.b64encode(text.encode()).decode()
return f'<a href="data:file/txt;base64,{b64}" download="{filename}">π₯ Download Notes</a>'
def extract_text_from_pdf(uploaded_file):
pdf_reader = PyPDF2.PdfReader(uploaded_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return text
def extract_text_from_word(uploaded_file):
doc = Document(uploaded_file)
text = ""
for para in doc.paragraphs:
text += para.text
return text
# --- Daily News Summary ---
if menu == "Daily News Summary":
st.header("π° AI-Powered News Summarizer")
user_news = st.text_area("Paste or write today's news:", height=300)
lang = st.radio("Language", ["English", "Urdu"])
if st.button("Summarize") and user_news:
summary = summarizer(user_news, max_length=500, min_length=100, do_sample=False)[0]['summary_text']
if lang == "Urdu":
translated = translator_en_ur(summary)[0]['translation_text']
st.success("Summary in Urdu:")
st.write(translated)
if st.button("Save Urdu Summary"):
save_note(f"Urdu Summary - {datetime.now().date()}", translated)
else:
st.success("Summary in English:")
st.write(summary)
if st.button("Save English Summary"):
save_note(f"English Summary - {datetime.now().date()}", summary)
# --- Precis Evaluation ---
elif menu == "Precis Evaluation":
st.header("βοΈ Precis Evaluation Tool")
precis_input = st.text_area("Enter your precis for evaluation:", height=300)
if st.button("Evaluate Precis") and precis_input:
corrected = grammar_corrector("grammar: " + precis_input)[0]['generated_text']
st.success("β
Corrected Precis:")
st.write(corrected)
if st.button("Save Precis"):
save_note(f"Precis - {datetime.now().date()}", corrected)
# --- Essay Feedback ---
elif menu == "Essay Feedback":
st.header("ποΈ Essay Evaluation Tool")
essay_input = st.text_area("Paste your essay here:", height=400)
if st.button("Evaluate Essay") and essay_input:
corrected = grammar_corrector("grammar: " + essay_input)[0]['generated_text']
st.success("β
Suggestions:")
st.write(corrected)
if st.button("Save Essay"):
save_note(f"Essay - {datetime.now().date()}", corrected)
# --- Saved Notes ---
elif menu == "Saved Notes":
st.header("πΎ Saved Notes")
notes = load_notes()
st.markdown(notes)
st.markdown(generate_download_link(notes, "css_notes.txt"), unsafe_allow_html=True)
# --- File Upload ---
elif menu == "File Upload":
st.header("π File Upload - PDF or Word")
uploaded_file = st.file_uploader("Upload a PDF or Word file", type=["pdf", "docx"])
if uploaded_file is not None:
file_type = uploaded_file.type
if file_type == "application/pdf":
file_text = extract_text_from_pdf(uploaded_file)
elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
file_text = extract_text_from_word(uploaded_file)
st.text_area("Extracted Text", file_text, height=300)
# Now you can apply the summarization, translation, or grammar correction on the extracted text
lang = st.radio("Language for Summary/Translation", ["English", "Urdu"])
if st.button("Summarize and Translate") and file_text:
summary = summarizer(file_text, max_length=500, min_length=100, do_sample=False)[0]['summary_text']
if lang == "Urdu":
translated = translator_en_ur(summary)[0]['translation_text']
st.success("Summary in Urdu:")
st.write(translated)
else:
st.success("Summary in English:")
st.write(summary)
|