Akhil Koduri commited on
Commit
44f8d36
·
verified ·
1 Parent(s): d69dcfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -67
app.py CHANGED
@@ -5,27 +5,19 @@ import docx
5
  from transformers import BertTokenizer, BertForSequenceClassification
6
  import torch
7
  from io import StringIO
8
- import openpyxl
9
 
10
- # Load pre-trained model and tokenizer from Hugging Face
11
  model_name = "google/bert-base-uncased"
12
  tokenizer = BertTokenizer.from_pretrained(model_name)
13
  model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)
14
 
15
- # Custom labels for your classification task
16
  labels = {
17
- "LABEL_0": "Negative",
18
- "LABEL_1": "Positive"
19
  }
20
 
21
- # Streamlit app
22
- st.title("BERT Text Classification")
23
-
24
- st.write("This app uses a pre-trained BERT model to classify text into positive or negative sentiment.")
25
-
26
- # Input text area
27
- input_text = st.text_area("Enter text to classify")
28
-
29
  def classify_text(text):
30
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
31
  outputs = model(**inputs)
@@ -35,7 +27,7 @@ def classify_text(text):
35
  confidence_score = confidence_score.item()
36
  predicted_class = predicted_class.item()
37
 
38
- label = labels[f"LABEL_{predicted_class}"]
39
 
40
  # Adjust classification based on score
41
  if confidence_score < 0.75:
@@ -43,73 +35,37 @@ def classify_text(text):
43
 
44
  return label, confidence_score
45
 
46
- if st.button("Classify"):
47
- if input_text:
48
- # Perform classification
49
- label, score = classify_text(input_text)
 
 
50
 
51
- st.write(f"**Predicted Class:** {label}")
52
- st.write(f"**Confidence:** {score:.4f}")
53
- else:
54
- st.write("Please enter some text to classify.")
 
55
 
56
  # File upload section
57
  st.write("Upload a file for classification:")
58
-
59
  uploaded_file = st.file_uploader("Choose a file", type=["csv", "pdf", "txt", "doc", "docx", "xlsx"])
60
 
61
  if uploaded_file is not None:
62
  file_type = uploaded_file.type
63
  if file_type == "text/csv":
64
- # Process CSV file
65
  df = pd.read_csv(uploaded_file)
66
- if 'text' not in df.columns:
67
- st.write("The CSV file must contain a 'text' column.")
68
- else:
69
- df['Prediction'] = df['text'].apply(lambda x: classify_text(x)[0])
70
- df['Confidence'] = df['text'].apply(lambda x: classify_text(x)[1])
71
- st.write(df)
72
-
73
  elif file_type == "application/pdf":
74
- # Process PDF file
75
  with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
76
- text = ""
77
- for page in doc:
78
- text += page.get_text()
79
-
80
- # Perform classification
81
- label, score = classify_text(text)
82
-
83
- st.write(f"**Predicted Class for PDF:** {label}")
84
- st.write(f"**Confidence:** {score:.4f}")
85
-
86
  elif file_type == "text/plain":
87
- # Process TXT file
88
- text = StringIO(uploaded_file.getvalue().decode("utf-8")).read()
89
-
90
- # Perform classification
91
- label, score = classify_text(text)
92
-
93
- st.write(f"**Predicted Class for TXT:** {label}")
94
- st.write(f"**Confidence:** {score:.4f}")
95
-
96
  elif file_type in ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"]:
97
- # Process DOCX or DOC file
98
  doc = docx.Document(uploaded_file)
99
- text = "\n".join([para.text for para in doc.paragraphs])
100
 
101
- # Perform classification
102
  label, score = classify_text(text)
103
-
104
- st.write(f"**Predicted Class for DOC/DOCX:** {label}")
105
- st.write(f"**Confidence:** {score:.4f}")
106
-
107
- elif file_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
108
- # Process XLSX file
109
- df = pd.read_excel(uploaded_file)
110
- if 'text' not in df.columns:
111
- st.write("The XLSX file must contain a 'text' column.")
112
- else:
113
- df['Prediction'] = df['text'].apply(lambda x: classify_text(x)[0])
114
- df['Confidence'] = df['text'].apply(lambda x: classify_text(x)[1])
115
- st.write(df)
 
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
  tokenizer = BertTokenizer.from_pretrained(model_name)
12
  model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)
13
 
14
+ # Custom labels for classification
15
  labels = {
16
+ 0: "Negative",
17
+ 1: "Positive"
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)
 
27
  confidence_score = confidence_score.item()
28
  predicted_class = predicted_class.item()
29
 
30
+ label = labels[predicted_class]
31
 
32
  # Adjust classification based on score
33
  if confidence_score < 0.75:
 
35
 
36
  return label, confidence_score
37
 
38
+ # Streamlit app
39
+ st.title("BERT Text Classification")
40
+ st.write("This app classifies text using a pre-trained BERT model.")
41
+
42
+ # Input text area
43
+ input_text = st.text_area("Enter text to classify")
44
 
45
+ # Classification and display
46
+ if st.button("Classify") and input_text:
47
+ label, score = classify_text(input_text)
48
+ st.write(f"Predicted Class: {label}")
49
+ st.write(f"Confidence: {score:.4f}")
50
 
51
  # File upload section
52
  st.write("Upload a file for classification:")
 
53
  uploaded_file = st.file_uploader("Choose a file", type=["csv", "pdf", "txt", "doc", "docx", "xlsx"])
54
 
55
  if uploaded_file is not None:
56
  file_type = uploaded_file.type
57
  if file_type == "text/csv":
 
58
  df = pd.read_csv(uploaded_file)
 
 
 
 
 
 
 
59
  elif file_type == "application/pdf":
 
60
  with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
61
+ text = "".join(page.get_text() for page in doc)
 
 
 
 
 
 
 
 
 
62
  elif file_type == "text/plain":
63
+ text = uploaded_file.getvalue().decode("utf-8")
 
 
 
 
 
 
 
 
64
  elif file_type in ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"]:
 
65
  doc = docx.Document(uploaded_file)
66
+ text = "\n".join(para.text for para in doc.paragraphs)
67
 
68
+ if 'text' in locals():
69
  label, score = classify_text(text)
70
+ st.write(f"Predicted Class for {file_type}: {label}")
71
+ st.write(f"Confidence: {score:.4f}")