Spaces:
Sleeping
Sleeping
Akhil Koduri
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
# Load pre-trained model and tokenizer from Hugging Face
|
@@ -12,22 +14,54 @@ labels = {
|
|
12 |
}
|
13 |
|
14 |
# Streamlit app
|
15 |
-
st.title("Text Classification")
|
16 |
|
17 |
st.write("This app uses a pre-trained BERT model to classify text into positive or negative sentiment.")
|
18 |
|
|
|
19 |
input_text = st.text_area("Enter text to classify")
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
if st.button("Classify"):
|
22 |
if input_text:
|
23 |
# Perform classification
|
24 |
-
|
25 |
-
|
26 |
-
# Extract label and score
|
27 |
-
label = labels.get(result[0]['label'], result[0]['label'])
|
28 |
-
score = result[0]['score']
|
29 |
|
30 |
st.write(f"**Predicted Class:** {label}")
|
31 |
st.write(f"**Confidence:** {score:.4f}")
|
32 |
else:
|
33 |
st.write("Please enter some text to classify.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import fitz # PyMuPDF
|
4 |
from transformers import pipeline
|
5 |
|
6 |
# Load pre-trained model and tokenizer from Hugging Face
|
|
|
14 |
}
|
15 |
|
16 |
# Streamlit app
|
17 |
+
st.title("BERT Text Classification")
|
18 |
|
19 |
st.write("This app uses a pre-trained BERT model to classify text into positive or negative sentiment.")
|
20 |
|
21 |
+
# Input text area
|
22 |
input_text = st.text_area("Enter text to classify")
|
23 |
|
24 |
+
def classify_text(text):
|
25 |
+
result = pipe(text)[0]
|
26 |
+
label = labels.get(result['label'], result['label'])
|
27 |
+
score = result['score']
|
28 |
+
return label, score
|
29 |
+
|
30 |
if st.button("Classify"):
|
31 |
if input_text:
|
32 |
# Perform classification
|
33 |
+
label, score = classify_text(input_text)
|
|
|
|
|
|
|
|
|
34 |
|
35 |
st.write(f"**Predicted Class:** {label}")
|
36 |
st.write(f"**Confidence:** {score:.4f}")
|
37 |
else:
|
38 |
st.write("Please enter some text to classify.")
|
39 |
+
|
40 |
+
# File upload section
|
41 |
+
st.write("Upload a file for classification:")
|
42 |
+
|
43 |
+
uploaded_file = st.file_uploader("Choose a file", type=["csv", "pdf"])
|
44 |
+
|
45 |
+
if uploaded_file is not None:
|
46 |
+
if uploaded_file.type == "text/csv":
|
47 |
+
# Process CSV file
|
48 |
+
df = pd.read_csv(uploaded_file)
|
49 |
+
if 'text' not in df.columns:
|
50 |
+
st.write("The CSV file must contain a 'text' column.")
|
51 |
+
else:
|
52 |
+
df['Prediction'] = df['text'].apply(lambda x: classify_text(x)[0])
|
53 |
+
df['Confidence'] = df['text'].apply(lambda x: classify_text(x)[1])
|
54 |
+
st.write(df)
|
55 |
+
|
56 |
+
elif uploaded_file.type == "application/pdf":
|
57 |
+
# Process PDF file
|
58 |
+
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
59 |
+
text = ""
|
60 |
+
for page in doc:
|
61 |
+
text += page.get_text()
|
62 |
+
|
63 |
+
# Perform classification
|
64 |
+
label, score = classify_text(text)
|
65 |
+
|
66 |
+
st.write(f"**Predicted Class for PDF:** {label}")
|
67 |
+
st.write(f"**Confidence:** {score:.4f}")
|