Spaces:
Running
Running
vkovacs
commited on
Commit
Β·
aa975e0
1
Parent(s):
92ccc59
PoC
Browse files
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MORES demo
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.23.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
short_description: emotion classification
|
| 11 |
---
|
| 12 |
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import AutoModelForSequenceClassification
|
| 5 |
+
from transformers import AutoTokenizer
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
PATH = '/data/' # at least 150GB storage needs to be attached
|
| 9 |
+
os.environ['TRANSFORMERS_CACHE'] = PATH
|
| 10 |
+
os.environ['HF_HOME'] = PATH
|
| 11 |
+
os.environ['HF_DATASETS_CACHE'] = PATH
|
| 12 |
+
os.environ['TORCH_HOME'] = PATH
|
| 13 |
+
|
| 14 |
+
HF_TOKEN = os.environ["hf_read"]
|
| 15 |
+
|
| 16 |
+
SENTIMENT_LABEL_NAMES = {0: "Negative", 1: "No sentiment or Neutral sentiment", 2: "Positive"}
|
| 17 |
+
LANGUAGES = ["Czech", "English", "French", "German", "Hungarian", "Polish", "Slovakian"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def build_huggingface_path(language: str):
|
| 21 |
+
if language == "Czech" or language == "Slovakian":
|
| 22 |
+
return "visegradmedia-emotion/Emotion_RoBERTa_pooled_V4"
|
| 23 |
+
return "poltextlab/xlm-roberta-large-pooled-MORES"
|
| 24 |
+
|
| 25 |
+
def predict(text, model_id, tokenizer_id):
|
| 26 |
+
device = torch.device("cpu")
|
| 27 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id, low_cpu_mem_usage=True, device_map="auto", offload_folder="offload", token=HF_TOKEN)
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
|
| 29 |
+
model.to(device)
|
| 30 |
+
|
| 31 |
+
inputs = tokenizer(text,
|
| 32 |
+
max_length=512,
|
| 33 |
+
truncation=True,
|
| 34 |
+
padding="do_not_pad",
|
| 35 |
+
return_tensors="pt").to(device)
|
| 36 |
+
model.eval()
|
| 37 |
+
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
logits = model(**inputs).logits
|
| 40 |
+
|
| 41 |
+
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy().flatten()
|
| 42 |
+
output_pred = {model.config.id2label[i]: probs[i] for i in np.argsort(probs)[::-1]}
|
| 43 |
+
output_info = f'<p style="text-align: center; display: block">Prediction was made using the <a href="https://huggingface.co/{model_id}">{model_id}</a> model.</p>'
|
| 44 |
+
return output_pred, output_info
|
| 45 |
+
|
| 46 |
+
def predict_wrapper(text, language):
|
| 47 |
+
model_id = build_huggingface_path(language)
|
| 48 |
+
tokenizer_id = "xlm-roberta-large"
|
| 49 |
+
return predict(text, model_id, tokenizer_id)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Interface(
|
| 54 |
+
fn=predict_wrapper,
|
| 55 |
+
inputs=[gr.Textbox(lines=6, label="Input"),
|
| 56 |
+
gr.Dropdown(LANGUAGES, label="Language")],
|
| 57 |
+
outputs=[gr.Label(num_top_classes=3, label="Output"), gr.Markdown()])
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
torch==2.2.1
|
| 3 |
+
transformers==4.39.1
|
| 4 |
+
sentencepiece==0.2.0
|
| 5 |
+
accelerate
|
| 6 |
+
spacy
|
| 7 |
+
huspacy
|
utils.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
|
| 4 |
+
from interfaces.cap import languages as languages_cap
|
| 5 |
+
from interfaces.cap import domains as domains_cap
|
| 6 |
+
|
| 7 |
+
from interfaces.cap import build_huggingface_path as hf_cap_path
|
| 8 |
+
from interfaces.manifesto import build_huggingface_path as hf_manifesto_path
|
| 9 |
+
from interfaces.sentiment import build_huggingface_path as hf_sentiment_path
|
| 10 |
+
from interfaces.emotion import build_huggingface_path as hf_emotion_path
|
| 11 |
+
|
| 12 |
+
HF_TOKEN = os.environ["hf_read"]
|
| 13 |
+
|
| 14 |
+
# should be a temporary solution
|
| 15 |
+
models = [hf_manifesto_path(""), hf_sentiment_path(""), hf_emotion_path("")]
|
| 16 |
+
|
| 17 |
+
domains_cap = list(domains_cap.values())
|
| 18 |
+
for language in languages_cap:
|
| 19 |
+
for domain in domains_cap:
|
| 20 |
+
models.append(hf_cap_path(language, domain))
|
| 21 |
+
|
| 22 |
+
tokenizers = ["xlm-roberta-large"]
|
| 23 |
+
|
| 24 |
+
def download_hf_models():
|
| 25 |
+
for model_id in models:
|
| 26 |
+
AutoModelForSequenceClassification.from_pretrained(model_id, low_cpu_mem_usage=True, device_map="auto", offload_folder="offload",
|
| 27 |
+
token=HF_TOKEN)
|
| 28 |
+
for tokenizer_id in tokenizers:
|
| 29 |
+
AutoTokenizer.from_pretrained(tokenizer_id)
|
| 30 |
+
|