Spaces:
Sleeping
Sleeping
File size: 6,279 Bytes
d577809 3c3ebf3 d577809 2289967 d577809 2289967 d577809 2289967 d577809 |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
import streamlit as st
from transformers import pipeline
import torch
import PyPDF2
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:
text_classification_model = pipeline(
"text-classification",
model="distilbert-base-uncased-finetuned-sst-2-english"
)
question_answering_model = pipeline(
"question-answering",
model="distilbert-base-uncased-distilled-squad"
)
translation_model = pipeline(
"translation",
model="Helsinki-NLP/opus-mt-en-fr"
)
summarization_model = pipeline(
"summarization",
model="facebook/bart-large-cnn"
)
except Exception as e:
raise RuntimeError(f"Failed to load models: {str(e)}")
return text_classification_model, question_answering_model, translation_model, summarization_model
def extract_text_from_pdf(uploaded_pdf):
try:
pdf_reader = PyPDF2.PdfReader(uploaded_pdf)
pdf_text = ""
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
pdf_text += page.extract_text()
return pdf_text
except Exception as e:
st.error(f"Error reading the PDF: {e}")
return None
try:
classification_model, qa_model, translation_model, 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",
["Question Answering", "Text Classification", "Language Translation", "Text Summarization"]
)
if option == "Question Answering":
st.title("Question Answering")
st.markdown("<h4 style='font-size: 20px;'>- because Google wasn't enough π</h4>", unsafe_allow_html=True)
uploaded_pdf = st.file_uploader("Upload a PDF file (optional)", type="pdf")
context_input = st.text_area("Enter context (a paragraph of text, or leave empty if using PDF):")
question = st.text_input("Enter your question:")
if uploaded_pdf:
context_input = extract_text_from_pdf(uploaded_pdf)
if st.button("Get Answer"):
with st.spinner('Getting answer...'):
try:
if context_input and question:
answer = qa_model(question=question, context=context_input)
st.write("Answer:", answer['answer'])
st.balloons()
else:
st.error("Please enter both context and a question.")
except Exception as e:
st.error(f"An error occurred: {e}")
elif option == "Text Classification":
st.title("Text Classification")
st.markdown("<h4 style='font-size: 20px;'>- where machines learn to hate spam as much we do π
</h4>", unsafe_allow_html=True)
text = st.text_area("Enter text for classification:")
if st.button("Classify Text"):
with st.spinner('Classifying text...'):
try:
classification = classification_model(text)
st.json(classification)
st.balloons()
except Exception as e:
st.error(f"An error occurred: {e}")
elif option == "Language Translation":
st.title("Language Translation (English to Multiple Languages)")
st.markdown("<h4 style='font-size: 20px;'>- when 'translate' is the only button you know π</h4>", unsafe_allow_html=True)
target_language = st.selectbox("Choose target language", ["French", "Spanish", "German", "Italian", "Portuguese", "Hindi"])
language_models = {
"French": "Helsinki-NLP/opus-mt-en-fr",
"Spanish": "Helsinki-NLP/opus-mt-en-es",
"German": "Helsinki-NLP/opus-mt-en-de",
"Italian": "Helsinki-NLP/opus-mt-en-it",
"Portuguese": "Helsinki-NLP/opus-mt-en-pt",
"Hindi": "Helsinki-NLP/opus-mt-en-hi"
}
selected_model = language_models.get(target_language)
if selected_model:
translation_model = pipeline("translation", model=selected_model)
text_to_translate = st.text_area(f"Enter text to translate from English to {target_language}:")
if st.button("Translate"):
with st.spinner('Translating text...'):
try:
if text_to_translate:
translated_text = translation_model(text_to_translate)
st.write(f"Translated Text ({target_language}):", translated_text[0]['translation_text'])
st.balloons()
else:
st.error("Please enter text to translate.")
except Exception as e:
st.error(f"An error occurred: {e}")
elif option == "Text Summarization":
st.title("Text Summarization")
st.markdown("<h4 style='font-size: 20px;'>- because who needs to read the whole article, anyway? π₯΅</h4>", unsafe_allow_html=True)
uploaded_pdf = st.file_uploader("Upload a PDF file (optional)", type="pdf")
text_to_summarize = st.text_area("Enter text to summarize (or leave empty if using PDF):")
if uploaded_pdf:
text_to_summarize = extract_text_from_pdf(uploaded_pdf)
if st.button("Summarize"):
with st.spinner('Summarizing text...'):
try:
if text_to_summarize:
summary = summarization_model(text_to_summarize, max_length=130, min_length=30, do_sample=False)
st.write("Summary:", summary[0]['summary_text'])
st.balloons()
else:
st.error("Please enter text or upload a PDF for summarization.")
except Exception as e:
st.error(f"An error occurred: {e}")
|