farooq-09 commited on
Commit
e46f1b6
1 Parent(s): a8e4ec2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import numpy as np
4
+
5
+ # Load the pre-trained text classification model from Hugging Face
6
+ model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)
7
+ tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
8
+
9
+ def classify_text(text):
10
+ # Preprocess the text input
11
+ encoded_text = tokenizer(text, truncation=True, padding=True, return_tensors="pt")
12
+
13
+ # Make predictions using the pre-trained model
14
+ with torch.no_grad():
15
+ output = model(**encoded_text)
16
+ logits = output.logits
17
+ predictions = np.argmax(logits, axis=1)
18
+
19
+ # Convert predictions to class labels
20
+ class_labels = ["positive", "negative"]
21
+ predicted_labels = [class_labels[i] for i in predictions]
22
+
23
+ # Return the predicted labels
24
+ return predicted_labels
25
+
26
+ # Define the Gradio interface
27
+ interface = gr.Interface(
28
+ fn=classify_text,
29
+ inputs=gr.inputs.Textbox(label="Enter text to classify:"),
30
+ outputs=gr.outputs.Label(label="Predicted Label:")
31
+ )
32
+
33
+ # Launch the Gradio interface
34
+ interface.launch()