syedabdullah32 commited on
Commit
65bdceb
·
1 Parent(s): 9ba9a2d

Create nlpquiz.py

Browse files
Files changed (1) hide show
  1. nlpquiz.py +38 -0
nlpquiz.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+
4
+ # Load the pre-trained model and tokenizer
5
+ model_name = "bert-base-uncased"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
+
9
+ # Define the prediction function
10
+ def classify_text(text):
11
+ # Tokenize the input text
12
+ encoded_text = tokenizer(text, truncation=True, padding=True, return_tensors="pt")
13
+
14
+ # Make predictions with the model
15
+ predictions = model(**encoded_text)
16
+ pred_labels = predictions.logits.argmax(-1).cpu().numpy()
17
+
18
+ # Get the predicted labels and their corresponding probabilities
19
+ labels = tokenizer.convert_ids_to_labels(pred_labels)
20
+ probs = predictions.logits.softmax(-1).cpu().numpy()[:, 1]
21
+
22
+ return labels, probs
23
+
24
+ # Create the Streamlit app
25
+ st.title("Text Classification App")
26
+
27
+ # Input field for user text
28
+ user_text = st.text_input("Enter text to classify:")
29
+
30
+ # Predict the classification labels and probabilities
31
+ if user_text:
32
+ labels, probs = classify_text(user_text)
33
+
34
+ # Display the classification results
35
+ st.header("Classification Results:")
36
+
37
+ for label, prob in zip(labels, probs):
38
+ st.write(f"Label: {label} (Probability: {prob:.3f})")