Spaces:
Running
Running
File size: 976 Bytes
b4c83a0 |
1 2 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 |
from fastapi import FastAPI
from transformers import BertForSequenceClassification, AutoTokenizer
import torch
# ✅ Load trained FinBERT model
MODEL_PATH = "hariharan220/finbert-stock-sentiment"
model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
# ✅ Define sentiment labels
labels = ["Negative", "Neutral", "Positive"]
# ✅ Create FastAPI app
app = FastAPI()
@app.get("/")
async def home():
return {"message": "Stock Sentiment Analysis API is running!"}
@app.post("/predict")
async def predict_sentiment(text: str):
"""Predicts sentiment of stock-related text using FinBERT"""
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
prediction = torch.argmax(logits, dim=1).item()
sentiment = labels[prediction]
return {"text": text, "sentiment": sentiment}
|