Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- App.py +89 -0
- requirments.txt +5 -0
- runtime.txt +0 -0
App.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Forex Sentiment API", version="1.0")
|
| 8 |
+
|
| 9 |
+
# ===============================
|
| 10 |
+
# Load Models
|
| 11 |
+
# ===============================
|
| 12 |
+
finbert_name = "ProsusAI/finbert"
|
| 13 |
+
longformer_name = "Miruzen/LongFormer_Skripsi"
|
| 14 |
+
|
| 15 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 16 |
+
|
| 17 |
+
print("📥 Loading FinBERT model...")
|
| 18 |
+
finbert = pipeline("text-classification",
|
| 19 |
+
model=finbert_name,
|
| 20 |
+
tokenizer=finbert_name,
|
| 21 |
+
return_all_scores=True,
|
| 22 |
+
device=device)
|
| 23 |
+
|
| 24 |
+
print("📥 Loading LongFormer model...")
|
| 25 |
+
longformer = pipeline("text-classification",
|
| 26 |
+
model=longformer_name,
|
| 27 |
+
tokenizer=longformer_name,
|
| 28 |
+
return_all_scores=True,
|
| 29 |
+
device=device)
|
| 30 |
+
|
| 31 |
+
# ===============================
|
| 32 |
+
# Input Schema
|
| 33 |
+
# ===============================
|
| 34 |
+
class InputData(BaseModel):
|
| 35 |
+
title: str | None = None
|
| 36 |
+
content: str | None = None
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ===============================
|
| 40 |
+
# Helper Functions
|
| 41 |
+
# ===============================
|
| 42 |
+
def extract_scores(predictions):
|
| 43 |
+
"""Convert HF model output into {positive, neutral, negative} dict."""
|
| 44 |
+
scores = {"positive": 0.0, "neutral": 0.0, "negative": 0.0}
|
| 45 |
+
for item in predictions[0]:
|
| 46 |
+
label = item["label"].lower()
|
| 47 |
+
if "pos" in label:
|
| 48 |
+
scores["positive"] = item["score"]
|
| 49 |
+
elif "neg" in label:
|
| 50 |
+
scores["negative"] = item["score"]
|
| 51 |
+
elif "neu" in label:
|
| 52 |
+
scores["neutral"] = item["score"]
|
| 53 |
+
dominant = max(scores, key=scores.get)
|
| 54 |
+
return {"label": dominant, "scores": scores}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# ===============================
|
| 58 |
+
# Main Endpoint
|
| 59 |
+
# ===============================
|
| 60 |
+
@app.post("/analyze")
|
| 61 |
+
def analyze(data: InputData):
|
| 62 |
+
result = {}
|
| 63 |
+
|
| 64 |
+
if data.title:
|
| 65 |
+
finbert_out = finbert(data.title)
|
| 66 |
+
result["title"] = extract_scores(finbert_out)
|
| 67 |
+
|
| 68 |
+
if data.content:
|
| 69 |
+
longformer_out = longformer(data.content)
|
| 70 |
+
result["content"] = extract_scores(longformer_out)
|
| 71 |
+
|
| 72 |
+
# Gabungkan menjadi mood_score sederhana
|
| 73 |
+
mood_score = (
|
| 74 |
+
result.get("title", {}).get("scores", {}).get("positive", 0)
|
| 75 |
+
+ result.get("content", {}).get("scores", {}).get("positive", 0)
|
| 76 |
+
- result.get("title", {}).get("scores", {}).get("negative", 0)
|
| 77 |
+
- result.get("content", {}).get("scores", {}).get("negative", 0)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
return {
|
| 81 |
+
"mood_score": mood_score,
|
| 82 |
+
"details": result,
|
| 83 |
+
"status": "ok"
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@app.get("/")
|
| 88 |
+
def root():
|
| 89 |
+
return {"message": "Forex Sentiment API active!"}
|
requirments.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers==4.45.0
|
| 4 |
+
torch
|
| 5 |
+
pydantic
|
runtime.txt
ADDED
|
File without changes
|