Spaces:
Running
Running
create app.py
Browse files- app/app.py +26 -0
app/app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
app.add_middleware(
|
| 9 |
+
CORSMiddleware,
|
| 10 |
+
allow_origins=["*"],
|
| 11 |
+
allow_methods=["*"],
|
| 12 |
+
allow_headers=["*"],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
model = AutoModelForSequenceClassification.from_pretrained("iconic7/Finance_GPT_Sentiment_Analysis")
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained("iconic7/Finance_GPT_Sentiment_Analysis")
|
| 17 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 18 |
+
|
| 19 |
+
@app.post("/predict")
|
| 20 |
+
async def predict(request: Request):
|
| 21 |
+
data = await request.json()
|
| 22 |
+
text = data.get("text", "")
|
| 23 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 26 |
+
return {"result": {label: float(prob) for label, prob in zip(labels, probs[0])}}
|