FrancoMartino commited on
Commit
7067c1e
·
verified ·
1 Parent(s): fb79b27

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import LongformerTokenizerFast, LongformerForSequenceClassification
2
+ import torch
3
+ import gradio as gr
4
+ import requests
5
+ from bs4 import BeautifulSoup
6
+
7
+
8
+ def get_text_from_url(url):
9
+
10
+ headers = {
11
+ 'Accept-Language': 'en-US,en;q=0.9',
12
+ }
13
+ response = requests.get(url, headers=headers)
14
+ if response.status_code == 200:
15
+ soup = BeautifulSoup(response.content, 'html.parser')
16
+
17
+ texto = soup.get_text()
18
+
19
+ return texto
20
+ else:
21
+
22
+ print("Error al obtener la página:", response.status_code)
23
+ return None
24
+
25
+ classification_model_checkpoint = 'FrancoMartino/privacyPolicies_classification'
26
+ classification_tokenizer = LongformerTokenizerFast.from_pretrained(classification_model_checkpoint)
27
+ classification_model = LongformerForSequenceClassification.from_pretrained(classification_model_checkpoint)
28
+
29
+ def predict(url):
30
+ text = get_text_from_url(url)
31
+ inputs = classification_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=4096)
32
+ with torch.no_grad():
33
+ logits = classification_model(**inputs).logits
34
+ probabilities = torch.softmax(logits, dim=1)
35
+ prediction = probabilities[:,1].item()
36
+ return {'Risk Indicator': prediction}
37
+
38
+ examples_urls = [
39
+ ["https://help.instagram.com/155833707900388"],
40
+ ["https://www.apple.com/legal/privacy/en-ww/"],
41
+ ]
42
+
43
+ interface = gr.Interface(fn=predict, inputs="text",examples=examples_urls, outputs="label", title="Privacy Policy Risk Indicator", description="Enter a privacy policy URL to calculate risk.")
44
+ interface.launch()