Spaces:
Running
Running
File size: 3,912 Bytes
bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 bfba113 c279525 |
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 |
import streamlit as st
from transformers import pipeline
import PyPDF2
import docx
from io import BytesIO
st.set_page_config(
page_title="TextSphere",
page_icon="π€",
layout="wide",
initial_sidebar_state="expanded"
)
st.markdown("""
<style>
.footer {
position: fixed;
bottom: 0;
right: 0;
padding: 10px;
font-size: 16px;
color: #333;
background-color: #f1f1f1;
}
</style>
<div class="footer">
Made with β€οΈ by Baibhav Malviya
</div>
""", unsafe_allow_html=True)
@st.cache_resource
def load_models():
try:
summarization_model = pipeline(
"summarization",
model="facebook/bart-large-cnn"
)
except Exception as e:
raise RuntimeError(f"Failed to load models: {str(e)}")
return summarization_model
def extract_text_from_pdf(uploaded_file):
try:
pdf_reader = PyPDF2.PdfReader(uploaded_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() or "" # Ensure we avoid NoneType issues
return text.strip()
except Exception as e:
st.error(f"Error reading the PDF: {e}")
return None
def extract_text_from_docx(uploaded_file):
try:
doc = docx.Document(uploaded_file)
return "\n".join([para.text for para in doc.paragraphs])
except Exception as e:
st.error(f"Error reading the DOCX: {e}")
return None
def extract_text_from_txt(uploaded_file):
try:
return uploaded_file.read().decode("utf-8")
except Exception as e:
st.error(f"Error reading the TXT file: {e}")
return None
def extract_text_from_file(uploaded_file, file_type):
if file_type == "pdf":
return extract_text_from_pdf(uploaded_file)
elif file_type == "docx":
return extract_text_from_docx(uploaded_file)
elif file_type == "txt":
return extract_text_from_txt(uploaded_file)
return None
try:
summarization_model = load_models()
except Exception as e:
st.error(f"An error occurred while loading models: {e}")
st.sidebar.title("AI Solutions")
option = st.sidebar.selectbox(
"Choose a task",
["Text Summarization", "Question Answering", "Text Classification", "Language Translation"],
index=0 # Makes Text Summarization the default
)
if option == "Text Summarization":
st.title("Text Summarization")
st.markdown("<h4 style='font-size: 20px;'>- because who needs to read the whole document, anyway? π₯΅</h4>", unsafe_allow_html=True)
uploaded_file = st.file_uploader("Upload a document (PDF, DOCX, TXT) [Limit: 1024 Tokens]", type=["pdf", "docx", "txt"])
text_to_summarize = st.text_area("Enter text to summarize (or leave empty if uploading a file):")
if uploaded_file:
file_type = uploaded_file.name.split(".")[-1].lower()
text_to_summarize = extract_text_from_file(uploaded_file, file_type)
if st.button("Summarize"):
with st.spinner('Summarizing text...'):
try:
if text_to_summarize:
summary = summarization_model(text_to_summarize[:1024], max_length=300, min_length=50, do_sample=False)
st.write("Summary:", summary[0]['summary_text'])
st.balloons()
else:
st.error("Please enter text or upload a document for summarization.")
except Exception as e:
st.error(f"An error occurred: {e}")
elif option == "Question Answering":
st.title("Question Answering")
st.write("Coming soon... π")
elif option == "Text Classification":
st.title("Text Classification")
st.write("Coming soon... π")
elif option == "Language Translation":
st.title("Language Translation")
st.write("Coming soon... π")
|