Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,111 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Forex Sentiment API", version="2.0")
|
| 7 |
+
|
| 8 |
+
# ===============================
|
| 9 |
+
# Konfigurasi
|
| 10 |
+
# ===============================
|
| 11 |
+
HF_ROUTER_URL = "https://router.huggingface.co/hf-inference/models"
|
| 12 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 13 |
+
|
| 14 |
+
FINBERT_MODEL = "ProsusAI/finbert"
|
| 15 |
+
LONGFORMER_MODEL = "Miruzen/LongFormer_Skripsi"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# ===============================
|
| 19 |
+
# Input Schema
|
| 20 |
+
# ===============================
|
| 21 |
+
class InputData(BaseModel):
|
| 22 |
+
title: str | None = None
|
| 23 |
+
content: str | None = None
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# ===============================
|
| 27 |
+
# Helper Functions
|
| 28 |
+
# ===============================
|
| 29 |
+
def call_hf_model(model_name: str, text: str):
|
| 30 |
+
"""Kirim teks ke model Hugging Face menggunakan router API baru"""
|
| 31 |
+
headers = {
|
| 32 |
+
"Authorization": f"Bearer {HF_API_KEY}",
|
| 33 |
+
"Content-Type": "application/json",
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
payload = {
|
| 37 |
+
"inputs": text,
|
| 38 |
+
"options": {"wait_for_model": True}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
url = f"{HF_ROUTER_URL}/{model_name}"
|
| 42 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 43 |
+
|
| 44 |
+
if response.status_code != 200:
|
| 45 |
+
raise Exception(
|
| 46 |
+
f"HF API error ({response.status_code}): {response.text}"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return response.json()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def extract_scores(predictions):
|
| 53 |
+
"""Convert HF model output into {positive, neutral, negative} dict."""
|
| 54 |
+
scores = {"positive": 0.0, "neutral": 0.0, "negative": 0.0}
|
| 55 |
+
|
| 56 |
+
# Router API sometimes returns either list of lists or single list
|
| 57 |
+
data = predictions[0] if isinstance(predictions, list) else predictions
|
| 58 |
+
if isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict):
|
| 59 |
+
for item in data:
|
| 60 |
+
label = item.get("label", "").lower()
|
| 61 |
+
if "pos" in label:
|
| 62 |
+
scores["positive"] = item["score"]
|
| 63 |
+
elif "neg" in label:
|
| 64 |
+
scores["negative"] = item["score"]
|
| 65 |
+
elif "neu" in label:
|
| 66 |
+
scores["neutral"] = item["score"]
|
| 67 |
+
|
| 68 |
+
dominant = max(scores, key=scores.get)
|
| 69 |
+
return {"label": dominant, "scores": scores}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# ===============================
|
| 73 |
+
# Main Endpoint
|
| 74 |
+
# ===============================
|
| 75 |
+
@app.post("/analyze")
|
| 76 |
+
def analyze(data: InputData):
|
| 77 |
+
result = {}
|
| 78 |
+
errors = []
|
| 79 |
+
|
| 80 |
+
if data.title:
|
| 81 |
+
try:
|
| 82 |
+
finbert_out = call_hf_model(FINBERT_MODEL, data.title)
|
| 83 |
+
result["title"] = extract_scores(finbert_out)
|
| 84 |
+
except Exception as e:
|
| 85 |
+
errors.append(f"Title analysis error: {e}")
|
| 86 |
+
|
| 87 |
+
if data.content:
|
| 88 |
+
try:
|
| 89 |
+
longformer_out = call_hf_model(LONGFORMER_MODEL, data.content)
|
| 90 |
+
result["content"] = extract_scores(longformer_out)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
errors.append(f"Content analysis error: {e}")
|
| 93 |
+
|
| 94 |
+
mood_score = (
|
| 95 |
+
result.get("title", {}).get("scores", {}).get("positive", 0)
|
| 96 |
+
+ result.get("content", {}).get("scores", {}).get("positive", 0)
|
| 97 |
+
- result.get("title", {}).get("scores", {}).get("negative", 0)
|
| 98 |
+
- result.get("content", {}).get("scores", {}).get("negative", 0)
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
return {
|
| 102 |
+
"mood_score": mood_score,
|
| 103 |
+
"details": result,
|
| 104 |
+
"errors": errors,
|
| 105 |
+
"status": "ok" if not errors else "partial"
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@app.get("/")
|
| 110 |
+
def root():
|
| 111 |
+
return {"message": "Forex Sentiment API active via router.huggingface.co!"}
|