import streamlit as st # Web App from main import classify #demo_phrases = """ Here are some examples: #this is a phrase #is it neutral #nothing else to say #man I'm so damn angry #sarcasm lol #I love this product #""" demo_phrases = pd.read_csv('./train.csv')['comment_text'].head(20).astype(str).str.cat(sep='\n') # title st.title("Sentiment Analysis") # subtitle st.markdown("## A selection of popular sentiment analysis models - hosted on 🤗 Spaces") model_name = st.selectbox( "Select a pre-trained model", [ "finiteautomata/bertweet-base-sentiment-analysis", "ahmedrachid/FinancialBERT-Sentiment-Analysis", "finiteautomata/beto-sentiment-analysis", "NativeVex/custom-fine-tuned" ], ) input_sentences = st.text_area("Sentences", value=demo_phrases, height=200) data = input_sentences.split("\n") from transformers import AutoTokenizer, AutoModelForSequenceClassification model_path = "bin/model4" model = AutoModelForSequenceClassification.from_pretrained(model_path) tokenizer = AutoTokenizer.from_pretrained(model_path) from typing import List import torch import numpy as np import pandas as pd def infer(text: str) -> List[float]: encoding = tokenizer(text, return_tensors="pt") encoding = {k: v.to(model.device) for k,v in encoding.items()} outputs = model(**encoding) logits = outputs.logits sigmoid = torch.nn.Sigmoid() probs = sigmoid(logits.squeeze().cpu()) predictions = np.zeros(probs.shape) predictions[np.where(probs >= 0.5)] = 1 predictions = pd.Series(predictions == 1) predictions.index = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"] return {"label": str(predictions), "score": str(probs)} def wrapper(*args, **kwargs): if args[0] != "NativeVex/custom-fine-tuned": return classify(*args, **kwargs) else: return infer(text=args[1]) if st.button("Classify"): st.write("Please allow a few minutes for the model to run/download") for i in range(len(data)): j = wrapper(model_name.strip(), data[i])[0] sentiment = j["label"] confidence = j["score"] st.write( f"{i}. {data[i]} :: Classification - {sentiment} with confidence {confidence}" ) st.markdown( "Link to the app - [image-to-text-app on 🤗 Spaces](https://huggingface.co/spaces/Amrrs/image-to-text-app)" )