Spaces:
Runtime error
Runtime error
Ahmad-Moiz
commited on
Commit
•
f127a74
1
Parent(s):
e552d27
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import PyPDF2
|
4 |
+
|
5 |
+
# Function to extract text from PDF
|
6 |
+
def extract_text_from_pdf(pdf_file):
|
7 |
+
text = ""
|
8 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
9 |
+
for page_num in range(len(pdf_reader.pages)):
|
10 |
+
page = pdf_reader.getPage(page_num)
|
11 |
+
text += page.extractText()
|
12 |
+
return text
|
13 |
+
|
14 |
+
# Streamlit app
|
15 |
+
def main():
|
16 |
+
st.title('PDF Text Extraction')
|
17 |
+
|
18 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
19 |
+
if uploaded_file is not None:
|
20 |
+
st.write("File uploaded successfully!")
|
21 |
+
|
22 |
+
# Extract text when file is uploaded
|
23 |
+
text = extract_text_from_pdf(uploaded_file)
|
24 |
+
|
25 |
+
st.write("### Extracted Text:")
|
26 |
+
st.write(text)
|
27 |
+
|
28 |
+
# Use Hugging Face's pipeline for further NLP tasks
|
29 |
+
st.write("### NLP Analysis:")
|
30 |
+
nlp_task = st.selectbox("Select NLP Task", ["Named Entity Recognition", "Sentiment Analysis"])
|
31 |
+
|
32 |
+
if nlp_task == "Named Entity Recognition":
|
33 |
+
ner = pipeline("ner")
|
34 |
+
entities = ner(text)
|
35 |
+
st.write(entities)
|
36 |
+
|
37 |
+
if nlp_task == "Sentiment Analysis":
|
38 |
+
sentiment_analysis = pipeline("sentiment-analysis")
|
39 |
+
sentiment = sentiment_analysis(text)
|
40 |
+
st.write(sentiment)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
main()
|