Spaces:
Build error
Build error
Commit ·
06b376c
1
Parent(s): 85eca89
bertas defined
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from gradio_client import utils as client_utils
|
|
| 4 |
import spaces
|
| 5 |
import pandas as pd
|
| 6 |
import torch
|
| 7 |
-
from transformers import
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
import logging
|
| 10 |
import io
|
|
@@ -22,6 +22,7 @@ import logging
|
|
| 22 |
from typing import List, Set, Tuple
|
| 23 |
import asyncio
|
| 24 |
|
|
|
|
| 25 |
os.environ["ZEROGPU_ENABLED"] = "1"
|
| 26 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
| 27 |
|
|
@@ -322,9 +323,7 @@ class EventDetector:
|
|
| 322 |
|
| 323 |
self.rutranslator = ru_translate_fn
|
| 324 |
|
| 325 |
-
|
| 326 |
-
# For sentiment models
|
| 327 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 328 |
|
| 329 |
# Initialize FinBERT
|
| 330 |
finbert_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
|
|
@@ -345,7 +344,44 @@ class EventDetector:
|
|
| 345 |
|
| 346 |
self.finbert = lambda text: [finbert_sentiment(text)]
|
| 347 |
|
| 348 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
# Initialize MT5 model with safetensors
|
| 351 |
self.model_name = "google/mt5-small"
|
|
|
|
| 4 |
import spaces
|
| 5 |
import pandas as pd
|
| 6 |
import torch
|
| 7 |
+
from transformers import AutoModelForSequenceClassification, AutoModelForSeq2SeqLM, AutoTokenizer, AutoModel
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
import logging
|
| 10 |
import io
|
|
|
|
| 22 |
from typing import List, Set, Tuple
|
| 23 |
import asyncio
|
| 24 |
|
| 25 |
+
|
| 26 |
os.environ["ZEROGPU_ENABLED"] = "1"
|
| 27 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
| 28 |
|
|
|
|
| 323 |
|
| 324 |
self.rutranslator = ru_translate_fn
|
| 325 |
|
| 326 |
+
|
|
|
|
|
|
|
| 327 |
|
| 328 |
# Initialize FinBERT
|
| 329 |
finbert_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
|
|
|
|
| 344 |
|
| 345 |
self.finbert = lambda text: [finbert_sentiment(text)]
|
| 346 |
|
| 347 |
+
# Initialize RoBERTa
|
| 348 |
+
roberta_tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 349 |
+
roberta_model = AutoModelForSequenceClassification.from_pretrained(
|
| 350 |
+
"cardiffnlp/twitter-roberta-base-sentiment",
|
| 351 |
+
use_safetensors=True,
|
| 352 |
+
device_map=device
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
# Define custom pipeline instead of using transformers pipeline
|
| 356 |
+
def roberta_sentiment(text):
|
| 357 |
+
inputs = roberta_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 358 |
+
outputs = roberta_model(**inputs)
|
| 359 |
+
probs = outputs.logits.softmax(dim=1)[0]
|
| 360 |
+
pred_idx = probs.argmax().item()
|
| 361 |
+
labels = ["negative", "neutral", "positive"]
|
| 362 |
+
return {"label": labels[pred_idx], "score": probs[pred_idx].item()}
|
| 363 |
+
|
| 364 |
+
self.roberta = lambda text: [roberta_sentiment(text)]
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
# Initialize finbert_tone
|
| 368 |
+
finbert_tone_tokenizer = AutoTokenizer.from_pretrained("yiyanghkust/finbert-tone")
|
| 369 |
+
finbert_tone_model = AutoModelForSequenceClassification.from_pretrained(
|
| 370 |
+
"yiyanghkust/finbert-tone",
|
| 371 |
+
use_safetensors=True,
|
| 372 |
+
device_map=device
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
# Define custom pipeline instead of using transformers pipeline
|
| 376 |
+
def finbert_tone_sentiment(text):
|
| 377 |
+
inputs = finbert_tone_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 378 |
+
outputs = finbert_tone_model(**inputs)
|
| 379 |
+
probs = outputs.logits.softmax(dim=1)[0]
|
| 380 |
+
pred_idx = probs.argmax().item()
|
| 381 |
+
labels = ["negative", "neutral", "positive"]
|
| 382 |
+
return {"label": labels[pred_idx], "score": probs[pred_idx].item()}
|
| 383 |
+
|
| 384 |
+
self.finbert_tone = lambda text: [finbert_tone_sentiment(text)]
|
| 385 |
|
| 386 |
# Initialize MT5 model with safetensors
|
| 387 |
self.model_name = "google/mt5-small"
|