Ahmed-El-Sharkawy commited on
Commit
efdb077
·
verified ·
1 Parent(s): 053e0aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -1,32 +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()
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load the model from Hugging Face Hub
5
+ model_repo = "Ahmed-El-Sharkawy/my-sentiment-model" # Replace with your actual repo name
6
+ nlp_pipeline = pipeline("text-classification", model=model_repo)
7
+
8
+ # Gradio app function
9
+ def classify_text(input_text):
10
+ if input_text.strip() == "":
11
+ return "Please enter some text."
12
+ result = nlp_pipeline(input_text)
13
+ label = result[0]['label']
14
+ score = result[0]['score']
15
+
16
+ # Map Hugging Face labels to readable format
17
+ label_map = {'LABEL_0': "Negative", 'LABEL_1': "Positive"}
18
+ mapped_label = label_map.get(label, label)
19
+
20
+ return f"Prediction: {mapped_label}, Confidence Score: {score:.4f}"
21
+
22
+ # Gradio interface
23
+ iface = gr.Interface(
24
+ fn=classify_text,
25
+ inputs="text",
26
+ outputs="text",
27
+ title="Text Classification App",
28
+ description="Classify text as Positive (1) or Negative (0)"
29
+ )
30
+
31
+ # Launch the app
32
+ iface.launch()