File size: 3,189 Bytes
76c6334
1e1d870
dccce3b
 
 
eefc793
 
 
 
 
 
 
 
 
 
dccce3b
eefc793
 
 
 
 
 
dccce3b
 
 
eefc793
 
 
 
dccce3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eefc793
1e1d870
 
dccce3b
eefc793
 
 
dccce3b
1e1d870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eefc793
23e307e
1e1d870
 
 
 
23e307e
1e1d870
 
 
 
4066f8e
de1b425
eefc793
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import os
import torch
import torch
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM, RobertaTokenizer, RobertaForSequenceClassification

# Define a function for text summarization using GPT
def summarize_text(text, max_length=1024):
    tokenizer = AutoTokenizer.from_pretrained("kabita-choudhary/finetuned-bart-for-conversation-summary")
    model = AutoModelForSeq2SeqLM.from_pretrained("kabita-choudhary/finetuned-bart-for-conversation-summary")

    # Tokenize input text
    input_ids = tokenizer.encode(text, return_tensors='pt', max_length=1024, truncation=True)

    # Generate summary
    summary_ids = model.generate(input_ids, max_length=max_length, num_return_sequences=1, early_stopping=True)

    # Decode and return summary
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary

# Define a function to analyze text for potential adult content
def analyze_adult_content(text):
    # Load pre-trained RoBERTa model and tokenizer
    tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
    model = RobertaForSequenceClassification.from_pretrained('roberta-base')

    # 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

def text_analysis(text):  
    # Analyze sentiment
    sentiment_label = analyze_adult_content(text)
    sentiment = analyze_sentiment(text)
    summary = summarize_text(text)

    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:#cfb0b1>
            <h2>Text Summary</h2>
            <p>{}</p>
            </div>
            </body>
            </html>
            '''.format(sentiment_label, sentiment, summary)
    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()