Spaces:
No application file
No application file
Download app.py from sumit00763/sentiment: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/sumit00763/sentiment/resolve/main/app.py
- Command line
-
hf download hf://spaces/sumit00763/sentiment/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sumit00763/sentiment/resolve/main/app.py
1.21 kB
| from fastapi import FastAPI, Query | |
| from pydantic import BaseModel | |
| from typing import Optional | |
| import pandas as pd | |
| from EntitySentiment import entity_sentiment_single, load_pipeline | |
| import spacy | |
| try: | |
| nlp = spacy.load("en_core_web_sm") | |
| except OSError: | |
| import os | |
| os.system("python -m spacy download en_core_web_sm") | |
| nlp = spacy.load("en_core_web_sm") | |
| # Initialize FastAPI | |
| app = FastAPI( | |
| title="Entity-Level Financial Sentiment API", | |
| description="Runs entity-based sentiment analysis on financial texts using FinBERT + spaCy", | |
| version="1.0.0" | |
| ) | |
| # Load pipeline ONCE at startup (so models don’t reload every request) | |
| nlp, tokenizer, model, device, id2label = load_pipeline() | |
| class SentimentRequest(BaseModel): | |
| text: str | |
| entity_filter: Optional[str] = None | |
| def analyze_sentiment(req: SentimentRequest): | |
| df = entity_sentiment_single( | |
| text=req.text, | |
| entity_filter=req.entity_filter, | |
| finbert_model_name="yiyanghkust/finbert-tone", | |
| spacy_model_name="en_core_web_trf" | |
| ) | |
| return df.to_dict(orient="records") | |
| def root(): | |
| return {"message": "Entity-Level Financial Sentiment API is running!"} | |