Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import torch | |
# Load model directly | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
import torch | |
from transformers import RobertaTokenizer, RobertaForSequenceClassification | |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer | |
# Load pre-trained RoBERTa model and tokenizer | |
tokenizer = RobertaTokenizer.from_pretrained('roberta-base') | |
model = RobertaForSequenceClassification.from_pretrained('roberta-base') | |
# Define a function to analyze text for potential adult content | |
def analyze_adult_content(text): | |
# Tokenize input text | |
inputs = tokenizer(text, return_tensors='pt') | |
# Perform inference | |
outputs = model(**inputs) | |
# Get predicted label (0: Not Adult Content, 1: Adult Content) | |
predicted_label_idx = torch.argmax(outputs.logits).item() | |
predicted_label = model.config.id2label[predicted_label_idx] | |
return predicted_label | |
# Define a function to analyze the sentiment of the text using VADER | |
def analyze_sentiment(text): | |
analyzer = SentimentIntensityAnalyzer() | |
sentiment_scores = analyzer.polarity_scores(text) | |
# Determine sentiment label based on compound score | |
if sentiment_scores['compound'] >= 0.05: | |
sentiment_label = 'Positive' | |
elif sentiment_scores['compound'] <= -0.05: | |
sentiment_label = 'Negative' | |
else: | |
sentiment_label = 'Neutral' | |
return sentiment_label, sentiment_scores | |
# Example text | |
text = "I really enjoy watching this movie, it's so entertaining!" | |
# Analyze adult content | |
adult_content_label = analyze_adult_content(text) | |
print("Adult Content Label:", adult_content_label) | |
def text_analysis(text): | |
# Analyze sentiment | |
sentiment_label, sentiment_scores = analyze_sentiment(text) | |
print("Sentiment Label:", sentiment_label) | |
print("Sentiment Scores:", sentiment_scores) | |
html = '''<!doctype html> | |
<html> | |
<body> | |
<h1>Text Sentiment Analysis</h1> | |
<div style=background-color:#d9eee1> | |
<h2>Overall Sentiment</h2> | |
<p>{}</p> | |
</div> | |
<div style=background-color:#fff4a3> | |
<h2>Adult Content</h2> | |
<p>{}</p> | |
</div> | |
<div style=background-color:#ffc0c7> | |
<h2>Hate Speech</h2> | |
<p>{}</p> | |
</div> | |
<div style=background-color:#cfb0b1> | |
<h2>Text Summary</h2> | |
<p>{}</p> | |
</div> | |
</body> | |
</html> | |
'''.format(sentiment_label, sentiment_scores, "Gamma", "Theta") | |
return html | |
demo = gr.Interface( | |
text_analysis, | |
gr.Textbox(placeholder="Enter sentence here..."), | |
["html"], | |
examples=[ | |
["What a beautiful morning for a walk!"], | |
["It was the best of times, it was the worst of times."], | |
], | |
) | |
demo.launch() | |