alperugurcan commited on
Commit
b2784af
·
verified ·
1 Parent(s): 6850afa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import DistilBertTokenizer, DistilBertModel
4
+
5
+ class SimilarityPredictor:
6
+ def __init__(self):
7
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
+ self.model = DistilBertModel.from_pretrained('patent_similarity_model').to(self.device)
9
+ self.tokenizer = DistilBertTokenizer.from_pretrained('patent_similarity_model')
10
+ self.head = torch.nn.Sequential(torch.nn.Linear(768, 1), torch.nn.Sigmoid()).to(self.device)
11
+ self.head.load_state_dict(torch.load('patent_similarity_model/head.pt', map_location=self.device))
12
+
13
+ def predict(self, anchor, target):
14
+ self.model.eval()
15
+ with torch.no_grad():
16
+ encoded = self.tokenizer(
17
+ [anchor],
18
+ [target],
19
+ padding=True,
20
+ truncation=True,
21
+ max_length=64,
22
+ return_tensors='pt'
23
+ ).to(self.device)
24
+
25
+ output = self.head(self.model(**encoded)[0][:,0,:]).squeeze()
26
+ return float(output)
27
+
28
+ predictor = SimilarityPredictor()
29
+
30
+ # Örnek seçenekler
31
+ example_pairs = [
32
+ ["mobile phone", "cellphone"],
33
+ ["artificial intelligence", "machine learning"],
34
+ ["electric vehicle", "battery powered car"],
35
+ ["wireless communication", "radio transmission"],
36
+ ["solar panel", "photovoltaic cell"],
37
+ ["computer processor", "CPU"],
38
+ ["digital storage", "memory device"],
39
+ ["touch screen", "interactive display"],
40
+ ["biometric authentication", "fingerprint recognition"],
41
+ ["cloud computing", "remote server processing"]
42
+ ]
43
+
44
+ def predict_similarity(anchor, target):
45
+ score = predictor.predict(anchor, target)
46
+ return round(score, 3)
47
+
48
+ # Create Gradio interface with examples
49
+ iface = gr.Interface(
50
+ fn=predict_similarity,
51
+ inputs=[
52
+ gr.Textbox(label="Anchor Phrase", placeholder="Enter first phrase..."),
53
+ gr.Textbox(label="Target Phrase", placeholder="Enter second phrase...")
54
+ ],
55
+ outputs=gr.Number(label="Similarity Score (0-1)"),
56
+ title="Patent Phrase Similarity Checker",
57
+ description="""Compare the similarity between two patent phrases.
58
+
59
+ Score guide:
60
+ - 1.0: Very close match (exact or near-exact)
61
+ - 0.75: Close synonyms
62
+ - 0.5: Related terms
63
+ - 0.25: Somewhat related
64
+ - 0.0: Unrelated
65
+
66
+ Try the examples below or enter your own phrases!""",
67
+ examples=example_pairs,
68
+ theme="huggingface",
69
+ css="footer {display: none !important;}"
70
+ )
71
+
72
+ iface.launch()