swastikanata commited on
Commit
68a6256
·
1 Parent(s): ff62df8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -12
app.py CHANGED
@@ -1,27 +1,49 @@
 
1
  import gradio as gr
2
  import preprocessor as tweet_cleaner
3
- import requests
4
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
5
 
6
- pretrained_name = "w11wo/indonesian-roberta-base-sentiment-classifier"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- sentiment = pipeline(
9
- "sentiment-analysis",
10
- model=pretrained_name,
11
- tokenizer=pretrained_name,
12
- max_length=512,
13
- truncation=True,
14
- )
15
 
16
  def clean_tweet(tweet):
17
  return tweet_cleaner.clean(tweet)
18
 
19
  def get_sentiment(input_text):
20
- return sentiment(clean_tweet(input_text))
 
 
21
 
22
  iface = gr.Interface(fn = get_sentiment,
23
  inputs = 'text',
24
- outputs = ['text'],
25
  title = 'Analisis Sentimen Twitter',
26
  description="Dapatkan sentimen postiif, negatif, atau netral untuk tweet yang dimasukkan.")
27
 
 
1
+ import requests
2
  import gradio as gr
3
  import preprocessor as tweet_cleaner
4
+ # from transformers import pipeline
5
+
6
+ # pretrained_name = "w11wo/indonesian-roberta-base-sentiment-classifier"
7
+
8
+ # sentiment = pipeline(
9
+ # "sentiment-analysis",
10
+ # model=pretrained_name,
11
+ # tokenizer=pretrained_name,
12
+ # max_length=512,
13
+ # truncation=True,
14
+ # )
15
 
16
+ API_URL = "https://api-inference.huggingface.co/models/w11wo/indonesian-roberta-base-sentiment-classifier"
17
+ headers = {"Authorization": "Bearer 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"}
18
+
19
+ def format_sentiment(predictions):
20
+ formatted_output = dict()
21
+
22
+ for p in predictions:
23
+ if p['label'] == 'positive':
24
+ formatted_output['Positif'] = p['score']
25
+ elif p['label'] == 'negative':
26
+ formatted_output['Negatif'] = p['score']
27
+ else:
28
+ formatted_output['Netral'] = p['score']
29
+
30
+ return formatted_output
31
 
32
+ def query(payload):
33
+ response = requests.post(API_URL, headers=headers, json=payload)
34
+ return response.json()
 
 
 
 
35
 
36
  def clean_tweet(tweet):
37
  return tweet_cleaner.clean(tweet)
38
 
39
  def get_sentiment(input_text):
40
+ res = query({"inputs": clean_tweet(input_text)})
41
+ formatted_output = format_sentiment(res[0])
42
+ return formatted_output
43
 
44
  iface = gr.Interface(fn = get_sentiment,
45
  inputs = 'text',
46
+ outputs = ['label'],
47
  title = 'Analisis Sentimen Twitter',
48
  description="Dapatkan sentimen postiif, negatif, atau netral untuk tweet yang dimasukkan.")
49