Spaces:
Sleeping
Sleeping
fixing
Browse files- Procfile +1 -1
- api/predict.py +0 -20
- app.py +24 -8
- requirements.txt +1 -1
Procfile
CHANGED
@@ -1 +1 @@
|
|
1 |
-
web: gunicorn
|
|
|
1 |
+
web: gunicorn app:app
|
api/predict.py
DELETED
@@ -1,20 +0,0 @@
|
|
1 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
-
import torch
|
3 |
-
|
4 |
-
model_name = "w11wo/indonesian-roberta-base-sentiment-classifier"
|
5 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
6 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
7 |
-
|
8 |
-
labels = ["positive", "negative", "neutral"]
|
9 |
-
|
10 |
-
def softmax(logits):
|
11 |
-
exp_logits = torch.exp(logits)
|
12 |
-
return exp_logits / torch.sum(exp_logits, dim=1, keepdim=True)
|
13 |
-
|
14 |
-
def predict(text):
|
15 |
-
inputs = tokenizer(text, return_tensors="pt")
|
16 |
-
outputs = model(**inputs)
|
17 |
-
probabilities = softmax(outputs.logits)
|
18 |
-
probs = probabilities.detach().numpy().tolist()[0]
|
19 |
-
label = labels[probs.index(max(probs))]
|
20 |
-
return label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -1,14 +1,30 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
from
|
3 |
|
|
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
if __name__ == '__main__':
|
14 |
-
app.run(host='0.0.0.0', port=
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Inisialisasi aplikasi Flask
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# Inisialisasi pipeline untuk sentiment analysis
|
8 |
+
sentiment_analysis = pipeline("sentiment-analysis", model="w11wo/indonesian-roberta-base-sentiment-classifier")
|
9 |
+
|
10 |
+
@app.route('/')
|
11 |
+
def home():
|
12 |
+
return 'API Sentiment Analysis menggunakan w11wo/indonesian-roberta-base-sentiment-classifier'
|
13 |
+
|
14 |
+
@app.route('/analyze', methods=['POST'])
|
15 |
+
def analyze_sentiment():
|
16 |
+
try:
|
17 |
+
data = request.json
|
18 |
+
if 'text' not in data:
|
19 |
+
return jsonify({"error": "No text provided"}), 400
|
20 |
+
|
21 |
+
text = data['text']
|
22 |
+
result = sentiment_analysis(text)
|
23 |
+
|
24 |
+
return jsonify(result), 200
|
25 |
+
|
26 |
+
except Exception as e:
|
27 |
+
return jsonify({"error": str(e)}), 500
|
28 |
|
29 |
if __name__ == '__main__':
|
30 |
+
app.run(host='0.0.0.0', port=8080)
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
flask
|
2 |
transformers
|
3 |
torch
|
4 |
-
gunicorn
|
|
|
1 |
flask
|
2 |
transformers
|
3 |
torch
|
4 |
+
gunicorn
|