solewarrior commited on
Commit
3c3ebf3
Β·
verified Β·
1 Parent(s): d577809

Update ai_web_app.py

Browse files
Files changed (1) hide show
  1. ai_web_app.py +4 -30
ai_web_app.py CHANGED
@@ -4,12 +4,11 @@ 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("""
@@ -30,29 +29,24 @@ st.markdown("""
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"
@@ -63,8 +57,6 @@ def load_models():
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)
@@ -78,43 +70,35 @@ def extract_text_from_pdf(uploaded_pdf):
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.")
@@ -131,7 +115,6 @@ elif option == "Text Classification":
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}")
@@ -139,10 +122,8 @@ elif option == "Text Classification":
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",
@@ -152,7 +133,6 @@ elif option == "Language Translation":
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)
@@ -165,7 +145,6 @@ elif option == "Language Translation":
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.")
@@ -175,25 +154,20 @@ elif option == "Language Translation":
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.")
 
4
  import PyPDF2
5
  from io import BytesIO
6
 
 
7
  st.set_page_config(
8
+ page_title="TextSphere",
9
+ page_icon="πŸ€–",
10
+ layout="wide",
11
+ initial_sidebar_state="expanded"
12
  )
13
 
14
  st.markdown("""
 
29
  </div>
30
  """, unsafe_allow_html=True)
31
 
 
32
  @st.cache_resource
33
  def load_models():
34
  try:
 
35
  text_classification_model = pipeline(
36
  "text-classification",
37
  model="distilbert-base-uncased-finetuned-sst-2-english"
38
  )
39
 
 
40
  question_answering_model = pipeline(
41
  "question-answering",
42
  model="distilbert-base-uncased-distilled-squad"
43
  )
44
 
 
45
  translation_model = pipeline(
46
  "translation",
47
  model="Helsinki-NLP/opus-mt-en-fr"
48
  )
49
 
 
50
  summarization_model = pipeline(
51
  "summarization",
52
  model="facebook/bart-large-cnn"
 
57
 
58
  return text_classification_model, question_answering_model, translation_model, summarization_model
59
 
 
 
60
  def extract_text_from_pdf(uploaded_pdf):
61
  try:
62
  pdf_reader = PyPDF2.PdfReader(uploaded_pdf)
 
70
  return None
71
 
72
 
 
73
  try:
74
  classification_model, qa_model, translation_model, summarization_model = load_models()
75
  except Exception as e:
76
  st.error(f"An error occurred while loading models: {e}")
77
 
 
78
  st.sidebar.title("AI Solutions")
79
  option = st.sidebar.selectbox(
80
  "Choose a task",
81
  ["Question Answering", "Text Classification", "Language Translation", "Text Summarization"]
82
  )
83
 
 
84
  if option == "Question Answering":
85
  st.title("Question Answering")
86
  st.markdown("<h4 style='font-size: 20px;'>- because Google wasn't enough πŸ˜‰</h4>", unsafe_allow_html=True)
 
87
  uploaded_pdf = st.file_uploader("Upload a PDF file (optional)", type="pdf")
88
 
 
89
  context_input = st.text_area("Enter context (a paragraph of text, or leave empty if using PDF):")
90
  question = st.text_input("Enter your question:")
91
 
92
  if uploaded_pdf:
 
93
  context_input = extract_text_from_pdf(uploaded_pdf)
94
 
95
  if st.button("Get Answer"):
96
  with st.spinner('Getting answer...'):
97
  try:
98
  if context_input and question:
 
99
  answer = qa_model(question=question, context=context_input)
100
  st.write("Answer:", answer['answer'])
101
 
 
102
  st.balloons()
103
  else:
104
  st.error("Please enter both context and a question.")
 
115
  classification = classification_model(text)
116
  st.json(classification)
117
 
 
118
  st.balloons()
119
  except Exception as e:
120
  st.error(f"An error occurred: {e}")
 
122
  elif option == "Language Translation":
123
  st.title("Language Translation (English to Multiple Languages)")
124
  st.markdown("<h4 style='font-size: 20px;'>- when 'translate' is the only button you know 😁</h4>", unsafe_allow_html=True)
 
125
  target_language = st.selectbox("Choose target language", ["French", "Spanish", "German", "Italian", "Portuguese", "Hindi"])
126
 
 
127
  language_models = {
128
  "French": "Helsinki-NLP/opus-mt-en-fr",
129
  "Spanish": "Helsinki-NLP/opus-mt-en-es",
 
133
  "Hindi": "Helsinki-NLP/opus-mt-en-hi"
134
  }
135
 
 
136
  selected_model = language_models.get(target_language)
137
  if selected_model:
138
  translation_model = pipeline("translation", model=selected_model)
 
145
  translated_text = translation_model(text_to_translate)
146
  st.write(f"Translated Text ({target_language}):", translated_text[0]['translation_text'])
147
 
 
148
  st.balloons()
149
  else:
150
  st.error("Please enter text to translate.")
 
154
  elif option == "Text Summarization":
155
  st.title("Text Summarization")
156
  st.markdown("<h4 style='font-size: 20px;'>- because who needs to read the whole article, anyway? πŸ₯΅</h4>", unsafe_allow_html=True)
 
157
  uploaded_pdf = st.file_uploader("Upload a PDF file (optional)", type="pdf")
158
 
 
159
  text_to_summarize = st.text_area("Enter text to summarize (or leave empty if using PDF):")
160
 
161
  if uploaded_pdf:
 
162
  text_to_summarize = extract_text_from_pdf(uploaded_pdf)
163
 
164
  if st.button("Summarize"):
165
  with st.spinner('Summarizing text...'):
166
  try:
167
  if text_to_summarize:
 
168
  summary = summarization_model(text_to_summarize, max_length=130, min_length=30, do_sample=False)
169
  st.write("Summary:", summary[0]['summary_text'])
170
 
 
171
  st.balloons()
172
  else:
173
  st.error("Please enter text or upload a PDF for summarization.")