Spaces:
Runtime error
Runtime error
File size: 1,949 Bytes
b749f9b d74be1e b523b2d e0faa7c d763fab 0d92287 d74be1e 9565ec3 d74be1e 43f72c8 16683e1 b749f9b 16683e1 d74be1e 431582d 46bb59f ac68060 4e33088 cef4787 ac68060 cfc3e8a cef4787 cfc3e8a 1501319 89465fa d74be1e cef4787 d74be1e cef4787 |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
from transformers import RobertaTokenizer, AutoModelForSequenceClassification
from scipy.special import expit
import numpy as np
import os
import gradio as gr
import requests
from datetime import datetime
import transformers.utils.hub as _hub
_hub.list_repo_templates = lambda *args, **kwargs: [] # no-op
# set up model
authtoken = os.environ.get("TOKEN")
tokenizer = RobertaTokenizer.from_pretrained(
"guidecare/feelings_and_issues_large_v2",
use_safetensors=True,
use_auth_token=authtoken
)
tokenizer.do_lower_case = True
model = AutoModelForSequenceClassification.from_pretrained(
"guidecare/feelings_and_issues_large_v2",
use_safetensors=True,
use_auth_token=authtoken
)
all_label_names = list(model.config.id2label.values())
def predict(text):
probs = expit(model(**tokenizer([text], return_tensors="pt", padding=True)).logits.detach().numpy())
probs = [float(np.round(i, 2)) for i in probs[0]]
zipped_list = list(zip(all_label_names, probs))
print(text, zipped_list)
issues = [(i, j) for i, j in zipped_list if i.startswith('issue')]
feelings = [(i, j) for i, j in zipped_list if i.startswith('feeling')]
harm = [(i, j) for i, j in zipped_list if i.startswith('harm')]
sentiment = [(i, j) for i, j in zipped_list if i.startswith('sentiment')]
issues = sorted(issues, key=lambda x: x[1], reverse=True)
feelings = sorted(feelings, key=lambda x: x[1], reverse=True)
harm = sorted(harm, key=lambda x: x[1], reverse=True)
sentiment = sorted(sentiment, key=lambda x: x[1], reverse=True)
top = issues + feelings + harm + sentiment
d = {i: j for i, j in top}
return d
iface = gr.Interface(
fn=predict,
inputs=gr.Textbox(label="Enter text"),
outputs=gr.Label(label="Predictions"),
title="Emotion and Issue Predictor",
description="Enter a text to predict emotions and issues.",
)
if __name__ == "__main__":
iface.launch() |