RohitGuptaAI commited on
Commit
a32ba3c
·
1 Parent(s): e981d10

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.pipelines.image_segmentation import Predictions
2
+ import unidecode, re, unicodedata
3
+ from bs4 import BeautifulSoup
4
+ from urllib.request import urlopen
5
+ from urllib.parse import urlparse
6
+ from sklearn.metrics import confusion_matrix, accuracy_score
7
+ import torch.nn.functional as F
8
+ import gradio as gr
9
+
10
+ def check_by_url(txt_url):
11
+ #txt_url = "https://www.c-sharpcorner.com/article/how-to-add-multimedia-content-with-html/default.txt"
12
+ parsed_url = urlparse(txt_url)
13
+ url = f"{parsed_url.scheme}://{parsed_url.netloc}{parsed_url.path.rsplit('/', 1)[0]}/"
14
+ print(url)
15
+
16
+ new_data =[]
17
+ page = urlopen(url=url).read().decode("utf-8")
18
+ soup = BeautifulSoup(page, 'html.parser')
19
+ title = soup.find('title').get_text()
20
+
21
+ css_class_to_remove = "dp-highlighter" # Replace with the CSS class you want to remove
22
+ #Find <div> tags with the specified CSS class and remove their content
23
+ div_tags = soup.find_all(['code', 'pre'])
24
+ for div_tag in div_tags:
25
+ div_tag.clear()
26
+
27
+ div_tags = soup.find_all('div', class_=css_class_to_remove)
28
+ for div_tag in div_tags:
29
+ div_tag.clear()
30
+
31
+ # Fetch content of remaining tags
32
+ content_with_style = ""
33
+ p_tags_with_style = soup.find_all('p', style=True)
34
+ for p_tag in p_tags_with_style:
35
+ p_content = re.sub(r'\n', '', p_tag.get_text())
36
+ content_with_style += p_content
37
+
38
+ # Fetch content of <p> tags without style
39
+ content_without_style = ""
40
+ p_tags_without_style = soup.find_all('p', style=False)
41
+ for p_tag in p_tags_without_style:
42
+ p_content = re.sub(r'\n', '', p_tag.get_text())
43
+ content_without_style += p_content
44
+
45
+ # Replace Unicode characters in the content and remove duplicates
46
+ normalized_content_with_style = re.sub(r'\s+', ' ', content_with_style) # Remove extra spaces
47
+ normalized_content_with_style = normalized_content_with_style.replace('\r', '') # Replace '\r' characters
48
+ normalized_content_with_style = unicodedata.normalize('NFKD', normalized_content_with_style)
49
+ normalized_content_with_style = unidecode.unidecode(normalized_content_with_style)
50
+
51
+ normalized_content_without_style = re.sub(r'\s+', ' ', content_without_style) # Remove extra spaces
52
+ normalized_content_without_style = normalized_content_without_style.replace('\r', '') # Replace '\r' characters
53
+ normalized_content_without_style = unicodedata.normalize('NFKD', normalized_content_without_style)
54
+ normalized_content_without_style = unidecode.unidecode(normalized_content_without_style)
55
+
56
+ normalized_content_with_style += normalized_content_without_style
57
+ new_data = {"title": title, "content": normalized_content_with_style}
58
+
59
+ model = DistilBertForSequenceClassification.from_pretrained(Save_model)
60
+ tokenizer = DistilBertTokenizer.from_pretrained(Save_model)
61
+
62
+ test_encodings = tokenizer.encode_plus(
63
+ title,
64
+ truncation=True,
65
+ padding=True,
66
+ max_length=512,
67
+ return_tensors="pt"
68
+ )
69
+ model1=[]
70
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
71
+ test_input_ids = test_encodings["input_ids"].to(device)
72
+ test_attention_mask = test_encodings["attention_mask"].to(device)
73
+ with torch.no_grad():
74
+ model1= model.to(device)
75
+ model1.eval()
76
+ outputs= model1( test_input_ids, attention_mask=test_attention_mask)
77
+ logits = outputs.logits
78
+ predicted_labels = torch.argmax(logits, dim=1)
79
+ probabilities = F.softmax(logits, dim=1)
80
+ confidence_score_title = torch.max(probabilities, dim=1).values.tolist()
81
+ predicted_labels = torch.argmax(outputs.logits, dim=1)
82
+ label_mapping = {1: "SFW", 0: "NSFW"} # 1:True 0:false
83
+ predicted_label_title = label_mapping[predicted_labels.item()]
84
+
85
+
86
+ test_encodings = tokenizer.encode_plus(
87
+ normalized_content_with_style,
88
+ truncation=True,
89
+ padding=True,
90
+ max_length=512,
91
+ return_tensors="pt"
92
+ )
93
+ model1=[]
94
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
95
+ test_input_ids = test_encodings["input_ids"].to(device)
96
+ test_attention_mask = test_encodings["attention_mask"].to(device)
97
+ with torch.no_grad():
98
+ model1= model.to(device)
99
+ model1.eval()
100
+ outputs= model1( test_input_ids, attention_mask=test_attention_mask)
101
+ logits = outputs.logits
102
+ predicted_labels = torch.argmax(logits, dim=1)
103
+ probabilities = F.softmax(logits, dim=1)
104
+ confidence_scores_content = torch.max(probabilities, dim=1).values.tolist()
105
+ label_mapping = {1: "SFW", 0: "NSFW"} # 1:True 0:false
106
+ predicted_label_content = label_mapping[predicted_labels.item()]
107
+
108
+ return predicted_label_title, confidence_score_title, predicted_label_content, confidence_scores_content, new_data
109
+
110
+ def predict_2( url):
111
+ predicted_label_title, confidence_score_title,predicted_label_content, confidence_scores_content, new_data = check_by_url(url)
112
+ return predicted_label_title, confidence_score_title, predicted_label_content, confidence_scores_content, new_data
113
+
114
+ demo = gr.Interface(
115
+ fn=predict_2,
116
+ inputs= [
117
+ gr.inputs.Textbox(label="Enter URL"),
118
+
119
+ ],
120
+ outputs= [
121
+
122
+ gr.outputs.Textbox(label="Title_prediction"),
123
+ gr.outputs.Textbox(label="Title_confidence_score"),
124
+ gr.outputs.Textbox(label="Content_prediction"),
125
+ gr.outputs.Textbox(label="content_confidence_score"),
126
+ gr.outputs.Textbox(label="new_data").style(show_copy_button=True)
127
+ ],
128
+ )
129
+ demo.launch()