Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- README.md +2 -1
- app/main.py +28 -1
README.md
CHANGED
|
@@ -7,6 +7,7 @@ sdk: "docker"
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
|
|
|
| 10 |
# Sentiment API (FastAPI)
|
| 11 |
|
| 12 |
-
API simple pour analyser le sentiment d’un texte en français à l’aide de Transformers.
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
+
|
| 11 |
# Sentiment API (FastAPI)
|
| 12 |
|
| 13 |
+
API simple pour analyser le sentiment d’un texte en français à l’aide de Transformers.
|
app/main.py
CHANGED
|
@@ -2,11 +2,14 @@
|
|
| 2 |
import os
|
| 3 |
from typing import Optional, List
|
| 4 |
from functools import lru_cache
|
| 5 |
-
from fastapi import FastAPI
|
|
|
|
| 6 |
from pydantic import BaseModel, Field
|
| 7 |
from transformers import pipeline
|
| 8 |
from langdetect import detect
|
| 9 |
from .rules import rules_adjustment
|
|
|
|
|
|
|
| 10 |
|
| 11 |
os.environ.setdefault("TRANSFORMERS_CACHE", "/models")
|
| 12 |
|
|
@@ -16,6 +19,30 @@ FINBERT_MODEL = "yiyanghkust/finbert-tone"
|
|
| 16 |
|
| 17 |
app = FastAPI(title="Sentiment News API", version="1.0.0")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def clip(x: float, lo=-1.0, hi=1.0) -> float:
|
| 20 |
return max(lo, min(hi, x))
|
| 21 |
|
|
|
|
| 2 |
import os
|
| 3 |
from typing import Optional, List
|
| 4 |
from functools import lru_cache
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from pydantic import BaseModel, Field
|
| 8 |
from transformers import pipeline
|
| 9 |
from langdetect import detect
|
| 10 |
from .rules import rules_adjustment
|
| 11 |
+
import yfinance as yf
|
| 12 |
+
from datetime import datetime, timedelta
|
| 13 |
|
| 14 |
os.environ.setdefault("TRANSFORMERS_CACHE", "/models")
|
| 15 |
|
|
|
|
| 19 |
|
| 20 |
app = FastAPI(title="Sentiment News API", version="1.0.0")
|
| 21 |
|
| 22 |
+
@app.get("/api/stock-history/{ticker}")
|
| 23 |
+
def stock_history(ticker: str):
|
| 24 |
+
try:
|
| 25 |
+
end = datetime.now()
|
| 26 |
+
start = end - timedelta(days=7)
|
| 27 |
+
# Pour Euronext Paris, ajouter .PA si besoin
|
| 28 |
+
yf_ticker = ticker if '.' in ticker else f"{ticker}.PA"
|
| 29 |
+
data = yf.download(yf_ticker, start=start.strftime("%Y-%m-%d"), end=end.strftime("%Y-%m-%d"), interval="1d")
|
| 30 |
+
if data.empty:
|
| 31 |
+
raise HTTPException(status_code=404, detail="Aucune donnée trouvée")
|
| 32 |
+
# Formatage pour le front
|
| 33 |
+
series = [
|
| 34 |
+
{
|
| 35 |
+
"t": idx.strftime("%Y-%m-%dT%H:%M:%S"),
|
| 36 |
+
"price": float(row["Close"]),
|
| 37 |
+
"sentiment": 0.5, # mock
|
| 38 |
+
"volumeMentions": int(row["Volume"] or 0),
|
| 39 |
+
}
|
| 40 |
+
for idx, row in data.iterrows()
|
| 41 |
+
]
|
| 42 |
+
return {"series": series}
|
| 43 |
+
except Exception as e:
|
| 44 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 45 |
+
|
| 46 |
def clip(x: float, lo=-1.0, hi=1.0) -> float:
|
| 47 |
return max(lo, min(hi, x))
|
| 48 |
|