shrirangphadke commited on
Commit
eefc793
1 Parent(s): 1d3aabd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -1,20 +1,32 @@
1
  import gradio as gr
2
  import os
3
  import torch
4
-
5
- # Load model directly
6
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
-
8
  import torch
9
- from transformers import RobertaTokenizer, RobertaForSequenceClassification
10
  from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Load pre-trained RoBERTa model and tokenizer
13
- tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
14
- model = RobertaForSequenceClassification.from_pretrained('roberta-base')
 
 
 
15
 
16
  # Define a function to analyze text for potential adult content
17
  def analyze_adult_content(text):
 
 
 
 
18
  # Tokenize input text
19
  inputs = tokenizer(text, return_tensors='pt')
20
 
@@ -40,20 +52,13 @@ def analyze_sentiment(text):
40
  else:
41
  sentiment_label = 'Neutral'
42
 
43
- return sentiment_label, sentiment_scores
44
-
45
- # Example text
46
- text = "I really enjoy watching this movie, it's so entertaining!"
47
-
48
- # Analyze adult content
49
- adult_content_label = analyze_adult_content(text)
50
- print("Adult Content Label:", adult_content_label)
51
 
52
  def text_analysis(text):
53
  # Analyze sentiment
54
- sentiment_label, sentiment_scores = analyze_sentiment(text)
55
- print("Sentiment Label:", sentiment_label)
56
- print("Sentiment Scores:", sentiment_scores)
57
 
58
  html = '''<!doctype html>
59
  <html>
@@ -67,17 +72,13 @@ def text_analysis(text):
67
  <h2>Adult Content</h2>
68
  <p>{}</p>
69
  </div>
70
- <div style=background-color:#ffc0c7>
71
- <h2>Hate Speech</h2>
72
- <p>{}</p>
73
- </div>
74
  <div style=background-color:#cfb0b1>
75
  <h2>Text Summary</h2>
76
  <p>{}</p>
77
  </div>
78
  </body>
79
  </html>
80
- '''.format(sentiment_label, sentiment_scores, "Gamma", "Theta")
81
  return html
82
 
83
  demo = gr.Interface(
@@ -90,4 +91,4 @@ demo = gr.Interface(
90
  ],
91
  )
92
 
93
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
  import torch
 
 
 
 
4
  import torch
 
5
  from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
6
+ # Load model directly
7
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM, RobertaTokenizer, RobertaForSequenceClassification
8
+
9
+ # Define a function for text summarization using GPT
10
+ def summarize_text(text, max_length=1024):
11
+ tokenizer = AutoTokenizer.from_pretrained("kabita-choudhary/finetuned-bart-for-conversation-summary")
12
+ model = AutoModelForSeq2SeqLM.from_pretrained("kabita-choudhary/finetuned-bart-for-conversation-summary")
13
+
14
+ # Tokenize input text
15
+ input_ids = tokenizer.encode(text, return_tensors='pt', max_length=1024, truncation=True)
16
 
17
+ # Generate summary
18
+ summary_ids = model.generate(input_ids, max_length=max_length, num_return_sequences=1, early_stopping=True)
19
+
20
+ # Decode and return summary
21
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
22
+ return summary
23
 
24
  # Define a function to analyze text for potential adult content
25
  def analyze_adult_content(text):
26
+ # Load pre-trained RoBERTa model and tokenizer
27
+ tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
28
+ model = RobertaForSequenceClassification.from_pretrained('roberta-base')
29
+
30
  # Tokenize input text
31
  inputs = tokenizer(text, return_tensors='pt')
32
 
 
52
  else:
53
  sentiment_label = 'Neutral'
54
 
55
+ return sentiment_label
 
 
 
 
 
 
 
56
 
57
  def text_analysis(text):
58
  # Analyze sentiment
59
+ sentiment_label = analyze_adult_content(text)
60
+ sentiment = analyze_sentiment(text)
61
+ summary = summarize_text(text)
62
 
63
  html = '''<!doctype html>
64
  <html>
 
72
  <h2>Adult Content</h2>
73
  <p>{}</p>
74
  </div>
 
 
 
 
75
  <div style=background-color:#cfb0b1>
76
  <h2>Text Summary</h2>
77
  <p>{}</p>
78
  </div>
79
  </body>
80
  </html>
81
+ '''.format(sentiment_label, sentiment, summary)
82
  return html
83
 
84
  demo = gr.Interface(
 
91
  ],
92
  )
93
 
94
+ demo.launch()