Ahmed-El-Sharkawy commited on
Commit
193b653
·
verified ·
1 Parent(s): 53e7faf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, BertForSequenceClassification, BertTokenizer
3
+ import torch
4
+
5
+ # Load the model and tokenizer
6
+ model_path = 'D:\Study\university\Fourth Year\second semster\NLP\final exam\saved_model'
7
+ model = BertForSequenceClassification.from_pretrained(model_path)
8
+ tokenizer = BertTokenizer.from_pretrained(model_path)
9
+
10
+ # Create a pipeline for inference
11
+ nlp_pipeline = pipeline('text-classification', model=model, tokenizer=tokenizer)
12
+
13
+ # Gradio app function
14
+ def classify_text(input_text):
15
+ if input_text.strip() == "":
16
+ return "Please enter some text."
17
+ result = nlp_pipeline(input_text)
18
+ label = result[0]['label']
19
+ score = result[0]['score']
20
+
21
+ # Map labels to 1 for positive and 0 for negative
22
+ label_map = {'LABEL_0': " Negative ", 'LABEL_1': " Postive "}
23
+ mapped_label = label_map[label]
24
+
25
+ return f"Prediction: {mapped_label}, Confidence Score: {score:.4f}"
26
+
27
+ # Gradio interface
28
+ iface = gr.Interface(fn=classify_text, inputs="text", outputs="text", title="Text Classification App",
29
+ description="Classify text as Positive (1) or Negative (0)")
30
+
31
+ # Launch the Gradio app
32
+ iface.launch()