7jimmy commited on
Commit
bc7757c
·
1 Parent(s): 52af529

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import transformers
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ import numpy as np
5
+
6
+ # Load the pre-trained text classification model from Hugging Face
7
+ model_name = "bert-base-uncased"
8
+ num_labels = 2
9
+
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+
13
+ def classify_text(text):
14
+ # Preprocess the text input
15
+ encoded_text = tokenizer(text, truncation=True, padding=True, return_tensors="pt")
16
+
17
+ # Make predictions using the pre-trained model
18
+ with torch.no_grad():
19
+ outputs = model(**encoded_text)
20
+ logits = outputs.logits
21
+ predictions = np.argmax(logits, axis=1)
22
+
23
+ # Convert predictions to class labels
24
+ class_labels = ["positive", "negative"]
25
+ predicted_labels = [class_labels[i] for i in predictions]
26
+
27
+ # Return the predicted labels
28
+ return predicted_labels
29
+
30
+ # Initialize the Streamlit app
31
+ st.title("Text Classification Demo")
32
+
33
+ # Create the text input field
34
+ input_text = st.text_input("Enter text to classify:", "")
35
+
36
+ # Make predictions and display the results
37
+ if input_text:
38
+ predicted_labels = classify_text(input_text)
39
+ st.write("Predicted labels:", predicted_labels)