ramadhanlmzero commited on
Commit
af111bc
·
1 Parent(s): 315f320

third commit

Browse files
Files changed (1) hide show
  1. app.py +25 -4
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
- # Gunakan model IndoBERT yang sudah fine-tuned untuk sentimen
5
  analyzer = pipeline(
6
  "sentiment-analysis",
7
- model="w11wo/indonesian-roberta-base-sentiment-classifier"
8
  )
9
 
10
  def predict_sentiment(text):
@@ -13,12 +15,31 @@ def predict_sentiment(text):
13
  score = result['score']
14
  return f"Sentimen: {label}\nSkor: {score:.2f}"
15
 
 
16
  demo = gr.Interface(
17
  fn=predict_sentiment,
18
  inputs=gr.Textbox(label="Masukkan teks bahasa Indonesia"),
19
  outputs="text",
20
  title="Analisis Sentimen Bahasa Indonesia",
21
- description="Gunakan IndoBERT-RoBERTa untuk mendeteksi sentimen positif / negatif / netral"
22
  )
23
 
24
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from fastapi import FastAPI, Request
4
+ from fastapi.responses import JSONResponse
5
 
6
+ # Gunakan IndoBERT sentiment model
7
  analyzer = pipeline(
8
  "sentiment-analysis",
9
+ model="indobenchmark/indobert-base-p1"
10
  )
11
 
12
  def predict_sentiment(text):
 
15
  score = result['score']
16
  return f"Sentimen: {label}\nSkor: {score:.2f}"
17
 
18
+ # Gradio UI (masih bisa diakses di web)
19
  demo = gr.Interface(
20
  fn=predict_sentiment,
21
  inputs=gr.Textbox(label="Masukkan teks bahasa Indonesia"),
22
  outputs="text",
23
  title="Analisis Sentimen Bahasa Indonesia",
24
+ description="Gunakan IndoBERT untuk mendeteksi sentimen positif / negatif / netral"
25
  )
26
 
27
+ # FastAPI untuk REST API langsung
28
+ app = FastAPI()
29
+
30
+ @app.post("/predict")
31
+ async def api_predict(request: Request):
32
+ body = await request.json()
33
+ text = body.get("text")
34
+ if not text:
35
+ return JSONResponse({"error": "Field 'text' wajib ada"}, status_code=400)
36
+
37
+ result = predict_sentiment(text)
38
+ return {"result": result}
39
+
40
+ # Hubungkan Gradio dan FastAPI
41
+ gr.mount_gradio_app(app, demo, path="/")
42
+
43
+ if __name__ == "__main__":
44
+ import uvicorn
45
+ uvicorn.run(app, host="0.0.0.0", port=7860)