Spaces:
Sleeping
Sleeping
Akhil Koduri
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,68 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import fitz # PyMuPDF
|
4 |
-
import
|
5 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
6 |
-
import torch
|
7 |
-
from io import StringIO
|
8 |
|
9 |
-
# Load pre-trained model and tokenizer
|
10 |
-
model_name = "google/bert-base-uncased"
|
11 |
-
|
12 |
-
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)
|
13 |
|
14 |
-
# Custom labels for classification
|
15 |
labels = {
|
16 |
-
|
17 |
-
|
18 |
}
|
19 |
|
20 |
-
# Function to classify text
|
21 |
-
def classify_text(text):
|
22 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
23 |
-
outputs = model(**inputs)
|
24 |
-
logits = outputs.logits
|
25 |
-
probabilities = torch.softmax(logits, dim=-1)
|
26 |
-
confidence_score, predicted_class = torch.max(probabilities, dim=-1)
|
27 |
-
confidence_score = confidence_score.item()
|
28 |
-
predicted_class = predicted_class.item()
|
29 |
-
|
30 |
-
label = labels[predicted_class]
|
31 |
-
|
32 |
# Streamlit app
|
33 |
st.title("BERT Text Classification")
|
34 |
-
st.write("This app
|
35 |
|
36 |
# Input text area
|
37 |
input_text = st.text_area("Enter text to classify")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
label
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# File upload section
|
46 |
st.write("Upload a file for classification:")
|
47 |
-
uploaded_file = st.file_uploader("Choose a file", type=["csv", "pdf"
|
48 |
|
49 |
if uploaded_file is not None:
|
50 |
-
|
51 |
-
|
52 |
df = pd.read_csv(uploaded_file)
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
55 |
-
text = ""
|
56 |
-
|
57 |
-
|
58 |
-
elif file_type in ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"]:
|
59 |
-
doc = docx.Document(uploaded_file)
|
60 |
-
text = "\n".join(para.text for para in doc.paragraphs)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
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
|
7 |
+
model_name = "google-bert/bert-base-uncased"
|
8 |
+
pipe = pipeline("text-classification", model=model_name)
|
|
|
9 |
|
10 |
+
# Custom labels for your classification task
|
11 |
labels = {
|
12 |
+
"LABEL_0": "Negative",
|
13 |
+
"LABEL_1": "Positive"
|
14 |
}
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# Streamlit app
|
17 |
st.title("BERT Text Classification")
|
18 |
+
st.write("This app uses a pre-trained BERT model to classify text into positive or negative sentiment.")
|
19 |
|
20 |
# Input text area
|
21 |
input_text = st.text_area("Enter text to classify")
|
22 |
|
23 |
+
def classify_text(text):
|
24 |
+
result = pipe(text)[0]
|
25 |
+
label = labels.get(result['label'], result['label'])
|
26 |
+
score = result['score']
|
27 |
+
|
28 |
+
# Adjust classification based on score
|
29 |
+
if score < 0.75:
|
30 |
+
label = "Negative"
|
31 |
+
|
32 |
+
return label, score
|
33 |
+
|
34 |
+
if st.button("Classify"):
|
35 |
+
if input_text:
|
36 |
+
# Perform classification
|
37 |
+
label, score = classify_text(input_text)
|
38 |
+
st.write(f"**Predicted Class:** {label}")
|
39 |
+
st.write(f"**Confidence:** {score:.4f}")
|
40 |
+
else:
|
41 |
+
st.write("Please enter some text to classify.")
|
42 |
|
43 |
# File upload section
|
44 |
st.write("Upload a file for classification:")
|
45 |
+
uploaded_file = st.file_uploader("Choose a file", type=["csv", "pdf"])
|
46 |
|
47 |
if uploaded_file is not None:
|
48 |
+
if uploaded_file.type == "text/csv":
|
49 |
+
# Process CSV file
|
50 |
df = pd.read_csv(uploaded_file)
|
51 |
+
if 'text' not in df.columns:
|
52 |
+
st.write("The CSV file must contain a 'text' column.")
|
53 |
+
else:
|
54 |
+
df['Prediction'] = df['text'].apply(lambda x: classify_text(x)[0])
|
55 |
+
df['Confidence'] = df['text'].apply(lambda x: classify_text(x)[1])
|
56 |
+
st.write(df)
|
57 |
+
|
58 |
+
elif uploaded_file.type == "application/pdf":
|
59 |
+
# Process PDF file
|
60 |
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
61 |
+
text = ""
|
62 |
+
for page in doc:
|
63 |
+
text += page.get_text()
|
|
|
|
|
|
|
64 |
|
65 |
+
# Perform classification
|
66 |
+
label, score = classify_text(text)
|
67 |
+
st.write(f"**Predicted Class for PDF:** {label}")
|
68 |
+
st.write(f"**Confidence:** {score:.4f}")
|