PRAli22 commited on
Commit
53f88bc
·
verified ·
1 Parent(s): 3fe6573

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -1,26 +1,38 @@
1
- from transformers import pipeline
2
-
3
- import torch
4
  import gradio as gr
5
 
6
- repository_id = "PRAli22/AraBert-Arabic-Sentiment-Analysis"
7
 
8
- def sentiment_predict(sentences):
9
- pipe = pipeline("sentiment-analysis", model=repository_id, return_all_scores=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- return pipe(sentences)
12
 
13
  css_code='body{background-image:url("https://media.istockphoto.com/id/1256252051/vector/people-using-online-translation-app.jpg?s=612x612&w=0&k=20&c=aa6ykHXnSwqKu31fFR6r6Y1bYMS5FMAU9yHqwwylA94=");}'
14
 
15
  demo = gr.Interface(
16
- fn=sentiment_predict,
17
  inputs=
18
  gr.Textbox(label="sentence", placeholder=" Enter the sentence "),
19
 
20
 
21
- outputs=[gr.Textbox(label="entity name")],
22
- title="Arabic Named Entity Recognition",
23
- description= "This is Arabic Named Entity Recognition System, it takes an arabian sentence as input and returns every entity name within it",
24
  css = css_code
25
  )
26
  demo.launch()
 
1
+ from transformers import AutoConfig, AutoModelForTokenClassification, AutoTokenizer
2
+ import numpy as np
 
3
  import gradio as gr
4
 
 
5
 
6
+ tokenizer = AutoTokenizer.from_pretrained("PRAli22/AraBert-Arabic-Sentiment-Analysis" )
7
+ model = AutoModelForSequenceClassification.from_pretrained("PRAli22/AraBert-Arabic-Sentiment-Analysis")
8
+
9
+ def classify_sentiment(text):
10
+
11
+ # Tokenize the text
12
+ inputs = tokenizer(text, return_tensors="pt")
13
+
14
+ # Get model predictions
15
+ outputs = model(**inputs)
16
+ predicted_label_index = np.argmax(outputs[0].detach().numpy()).item()
17
+
18
+ # Retrieve label names from the model's config
19
+ label_names = {0: 'Positive', 1: 'Negative', 2: 'Neutral', 3: 'Mixed'}
20
+
21
+ predicted_label = label_names[predicted_label_index]
22
 
23
+ return predicted_label
24
 
25
  css_code='body{background-image:url("https://media.istockphoto.com/id/1256252051/vector/people-using-online-translation-app.jpg?s=612x612&w=0&k=20&c=aa6ykHXnSwqKu31fFR6r6Y1bYMS5FMAU9yHqwwylA94=");}'
26
 
27
  demo = gr.Interface(
28
+ fn=classify_sentiment,
29
  inputs=
30
  gr.Textbox(label="sentence", placeholder=" Enter the sentence "),
31
 
32
 
33
+ outputs=[gr.Textbox(label="the sentiment")],
34
+ title="Arabic Sentiment Analyzer",
35
+ description= "This is Arabic Sentiment Analyzer, it takes an arabian sentence as input and returns the sentiment behind it",
36
  css = css_code
37
  )
38
  demo.launch()