Spaces:
Sleeping
Create ai_web_app.py
Browse filesTextSphere is an interactive AI-powered web app built using Streamlit and Hugging Face's Transformers. It offers four powerful AI-driven tasks:
Question Answering: Upload a PDF or enter text to ask questions and get instant answers. It's like having your own AI-powered search engine, perfect for exploring documents!
Text Classification: Classify your text as positive or negative using sentiment analysis. It's a great tool for analyzing reviews, opinions, or feedback!
Language Translation: Translate English text into multiple languages, including French, Spanish, German, Portuguese, and Hindi. Break the language barrier with ease!
Text Summarization: Upload a PDF or input text, and let the app summarize long documents or articles into concise, readable summaries.
Features:
Streamlit-based UI: Easy-to-use interface with an interactive sidebar for navigation.
AI Models: Powered by Hugging Face models for natural language processing tasks.
PDF Upload: Upload PDFs for Question Answering and Text Summarization.
Customization: Personalized with the app's title, theme, and footer credits.
Made with ❤️ by Baibhav Malviya.
- ai_web_app.py +201 -0
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import PyPDF2
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
# Set page configuration
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="TextSphere", # Title of the page
|
10 |
+
page_icon="🤖", # Icon to be displayed in the browser tab
|
11 |
+
layout="wide", # Layout: can be 'wide' or 'centered'
|
12 |
+
initial_sidebar_state="expanded" # Sidebar state: can be 'expanded' or 'collapsed'
|
13 |
+
)
|
14 |
+
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
.footer {
|
18 |
+
position: fixed;
|
19 |
+
bottom: 0;
|
20 |
+
width: 100%;
|
21 |
+
text-align: center;
|
22 |
+
background-color: #f1f1f1;
|
23 |
+
padding: 10px;
|
24 |
+
font-size: 16px;
|
25 |
+
color: #333;
|
26 |
+
}
|
27 |
+
</style>
|
28 |
+
<div class="footer">
|
29 |
+
Made with ❤️ by Baibhav Malviya
|
30 |
+
</div>
|
31 |
+
""", unsafe_allow_html=True)
|
32 |
+
|
33 |
+
# Load models from Hugging Face
|
34 |
+
@st.cache_resource
|
35 |
+
def load_models():
|
36 |
+
try:
|
37 |
+
# Load DistilBERT for text classification
|
38 |
+
text_classification_model = pipeline(
|
39 |
+
"text-classification",
|
40 |
+
model="distilbert-base-uncased-finetuned-sst-2-english"
|
41 |
+
)
|
42 |
+
|
43 |
+
# Load Question Answering model
|
44 |
+
question_answering_model = pipeline(
|
45 |
+
"question-answering",
|
46 |
+
model="distilbert-base-uncased-distilled-squad"
|
47 |
+
)
|
48 |
+
|
49 |
+
# Load Translation model
|
50 |
+
translation_model = pipeline(
|
51 |
+
"translation",
|
52 |
+
model="Helsinki-NLP/opus-mt-en-fr"
|
53 |
+
)
|
54 |
+
|
55 |
+
# Load Summarization model
|
56 |
+
summarization_model = pipeline(
|
57 |
+
"summarization",
|
58 |
+
model="facebook/bart-large-cnn"
|
59 |
+
)
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
raise RuntimeError(f"Failed to load models: {str(e)}")
|
63 |
+
|
64 |
+
return text_classification_model, question_answering_model, translation_model, summarization_model
|
65 |
+
|
66 |
+
|
67 |
+
# Function to extract text from a PDF
|
68 |
+
def extract_text_from_pdf(uploaded_pdf):
|
69 |
+
try:
|
70 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_pdf)
|
71 |
+
pdf_text = ""
|
72 |
+
for page_num in range(len(pdf_reader.pages)):
|
73 |
+
page = pdf_reader.pages[page_num]
|
74 |
+
pdf_text += page.extract_text()
|
75 |
+
return pdf_text
|
76 |
+
except Exception as e:
|
77 |
+
st.error(f"Error reading the PDF: {e}")
|
78 |
+
return None
|
79 |
+
|
80 |
+
|
81 |
+
# Load models
|
82 |
+
try:
|
83 |
+
classification_model, qa_model, translation_model, summarization_model = load_models()
|
84 |
+
except Exception as e:
|
85 |
+
st.error(f"An error occurred while loading models: {e}")
|
86 |
+
|
87 |
+
# Sidebar navigation
|
88 |
+
st.sidebar.title("AI Solutions")
|
89 |
+
option = st.sidebar.selectbox(
|
90 |
+
"Choose a task",
|
91 |
+
["Question Answering", "Text Classification", "Language Translation", "Text Summarization"]
|
92 |
+
)
|
93 |
+
|
94 |
+
# Page content based on the selected option
|
95 |
+
if option == "Question Answering":
|
96 |
+
st.title("Question Answering")
|
97 |
+
st.markdown("<h4 style='font-size: 20px;'>- because Google wasn't enough 😉</h4>", unsafe_allow_html=True)
|
98 |
+
# PDF upload section
|
99 |
+
uploaded_pdf = st.file_uploader("Upload a PDF file (optional)", type="pdf")
|
100 |
+
|
101 |
+
# Text input section (when PDF is not uploaded)
|
102 |
+
context_input = st.text_area("Enter context (a paragraph of text, or leave empty if using PDF):")
|
103 |
+
question = st.text_input("Enter your question:")
|
104 |
+
|
105 |
+
if uploaded_pdf:
|
106 |
+
# Extract text from PDF
|
107 |
+
context_input = extract_text_from_pdf(uploaded_pdf)
|
108 |
+
|
109 |
+
if st.button("Get Answer"):
|
110 |
+
with st.spinner('Getting answer...'):
|
111 |
+
try:
|
112 |
+
if context_input and question:
|
113 |
+
# Use the question answering model to find the answer
|
114 |
+
answer = qa_model(question=question, context=context_input)
|
115 |
+
st.write("Answer:", answer['answer'])
|
116 |
+
|
117 |
+
# Show Streamlit balloons after task completion
|
118 |
+
st.balloons()
|
119 |
+
else:
|
120 |
+
st.error("Please enter both context and a question.")
|
121 |
+
except Exception as e:
|
122 |
+
st.error(f"An error occurred: {e}")
|
123 |
+
|
124 |
+
elif option == "Text Classification":
|
125 |
+
st.title("Text Classification")
|
126 |
+
st.markdown("<h4 style='font-size: 20px;'>- where machines learn to hate spam as much we do 😅</h4>", unsafe_allow_html=True)
|
127 |
+
text = st.text_area("Enter text for classification:")
|
128 |
+
if st.button("Classify Text"):
|
129 |
+
with st.spinner('Classifying text...'):
|
130 |
+
try:
|
131 |
+
classification = classification_model(text)
|
132 |
+
st.json(classification)
|
133 |
+
|
134 |
+
# Show Streamlit balloons after task completion
|
135 |
+
st.balloons()
|
136 |
+
except Exception as e:
|
137 |
+
st.error(f"An error occurred: {e}")
|
138 |
+
|
139 |
+
elif option == "Language Translation":
|
140 |
+
st.title("Language Translation (English to Multiple Languages)")
|
141 |
+
st.markdown("<h4 style='font-size: 20px;'>- when 'translate' is the only button you know 😁</h4>", unsafe_allow_html=True)
|
142 |
+
# Language options for translation
|
143 |
+
target_language = st.selectbox("Choose target language", ["French", "Spanish", "German", "Italian", "Portuguese", "Hindi"])
|
144 |
+
|
145 |
+
# Map of selected language to corresponding Hugging Face translation model
|
146 |
+
language_models = {
|
147 |
+
"French": "Helsinki-NLP/opus-mt-en-fr",
|
148 |
+
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
149 |
+
"German": "Helsinki-NLP/opus-mt-en-de",
|
150 |
+
"Italian": "Helsinki-NLP/opus-mt-en-it",
|
151 |
+
"Portuguese": "Helsinki-NLP/opus-mt-en-pt",
|
152 |
+
"Hindi": "Helsinki-NLP/opus-mt-en-hi"
|
153 |
+
}
|
154 |
+
|
155 |
+
# Update translation model based on selected language
|
156 |
+
selected_model = language_models.get(target_language)
|
157 |
+
if selected_model:
|
158 |
+
translation_model = pipeline("translation", model=selected_model)
|
159 |
+
|
160 |
+
text_to_translate = st.text_area(f"Enter text to translate from English to {target_language}:")
|
161 |
+
if st.button("Translate"):
|
162 |
+
with st.spinner('Translating text...'):
|
163 |
+
try:
|
164 |
+
if text_to_translate:
|
165 |
+
translated_text = translation_model(text_to_translate)
|
166 |
+
st.write(f"Translated Text ({target_language}):", translated_text[0]['translation_text'])
|
167 |
+
|
168 |
+
# Show Streamlit balloons after task completion
|
169 |
+
st.balloons()
|
170 |
+
else:
|
171 |
+
st.error("Please enter text to translate.")
|
172 |
+
except Exception as e:
|
173 |
+
st.error(f"An error occurred: {e}")
|
174 |
+
|
175 |
+
elif option == "Text Summarization":
|
176 |
+
st.title("Text Summarization")
|
177 |
+
st.markdown("<h4 style='font-size: 20px;'>- because who needs to read the whole article, anyway? 🥵</h4>", unsafe_allow_html=True)
|
178 |
+
# PDF upload section
|
179 |
+
uploaded_pdf = st.file_uploader("Upload a PDF file (optional)", type="pdf")
|
180 |
+
|
181 |
+
# Text input section (when PDF is not uploaded)
|
182 |
+
text_to_summarize = st.text_area("Enter text to summarize (or leave empty if using PDF):")
|
183 |
+
|
184 |
+
if uploaded_pdf:
|
185 |
+
# Extract text from PDF
|
186 |
+
text_to_summarize = extract_text_from_pdf(uploaded_pdf)
|
187 |
+
|
188 |
+
if st.button("Summarize"):
|
189 |
+
with st.spinner('Summarizing text...'):
|
190 |
+
try:
|
191 |
+
if text_to_summarize:
|
192 |
+
# Use the summarization model to generate a summary
|
193 |
+
summary = summarization_model(text_to_summarize, max_length=130, min_length=30, do_sample=False)
|
194 |
+
st.write("Summary:", summary[0]['summary_text'])
|
195 |
+
|
196 |
+
# Show Streamlit balloons after task completion
|
197 |
+
st.balloons()
|
198 |
+
else:
|
199 |
+
st.error("Please enter text or upload a PDF for summarization.")
|
200 |
+
except Exception as e:
|
201 |
+
st.error(f"An error occurred: {e}")
|