Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
MODEL_NAME = "yiyanghkust/finbert-tone"
|
| 7 |
+
|
| 8 |
+
# Load FinBERT
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
+
|
| 12 |
+
def analyze_financial_text(text: str):
|
| 13 |
+
inputs = tokenizer(
|
| 14 |
+
text,
|
| 15 |
+
return_tensors="pt",
|
| 16 |
+
truncation=True,
|
| 17 |
+
max_length=512,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
|
| 23 |
+
probs = F.softmax(outputs.logits, dim=-1)[0]
|
| 24 |
+
|
| 25 |
+
id2label = model.config.id2label
|
| 26 |
+
result = {id2label[i]: float(prob) for i, prob in enumerate(probs)}
|
| 27 |
+
|
| 28 |
+
return result
|
| 29 |
+
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=analyze_financial_text,
|
| 32 |
+
inputs=gr.Textbox(
|
| 33 |
+
label="Enter financial news / earnings call text:",
|
| 34 |
+
lines=6,
|
| 35 |
+
placeholder="Paste financial text here..."
|
| 36 |
+
),
|
| 37 |
+
outputs=gr.Label(
|
| 38 |
+
label="Sentiment (Positive / Neutral / Negative)",
|
| 39 |
+
num_top_classes=3
|
| 40 |
+
),
|
| 41 |
+
title="FinBERT Financial Sentiment Analyzer",
|
| 42 |
+
description="Analyze the sentiment of financial news or corporate disclosures using the FinBERT model."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|