Spaces:
Runtime error
Runtime error
Alexander Junge
commited on
Commit
·
a644d5c
1
Parent(s):
edbc9de
Add app
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from scipy.spatial.distance import cosine
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
model = SentenceTransformer("AI-Growth-Lab/PatentSBERTa")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_sim(anchor: str, target: str) -> float:
|
| 10 |
+
anchor_embed = model.encode([anchor])
|
| 11 |
+
target_embed = model.encode([target])
|
| 12 |
+
return float(1 - cosine(anchor_embed, target_embed))
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
anchor_input = gr.inputs.Textbox(lines=1, placeholder="Anchor")
|
| 16 |
+
target_input = gr.inputs.Textbox(lines=1, placeholder="Target")
|
| 17 |
+
|
| 18 |
+
sim_output = gr.outputs.Textbox(type="number", label="Similarity")
|
| 19 |
+
|
| 20 |
+
examples = [
|
| 21 |
+
["renewable power", "renewable energy"],
|
| 22 |
+
["previously captured image", "image captured previously"],
|
| 23 |
+
["labeled ligand", "container labelling"],
|
| 24 |
+
["gold alloy", "platinum"],
|
| 25 |
+
["dissolve in glycol", "family gathering"],
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=get_sim,
|
| 30 |
+
inputs=[anchor_input, target_input],
|
| 31 |
+
outputs=sim_output,
|
| 32 |
+
examples=examples,
|
| 33 |
+
theme="grass",
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
app, local_url, share_url = iface.launch(enable_queue=True)
|