Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import fasttext
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
|
5 |
+
model_path = hf_hub_download(repo_id="LuminAISecurity/fasttext-english-nearest-neighbors-demo", filename="model.bin")
|
6 |
+
model = fasttext.load_model(model_path)
|
7 |
+
|
8 |
+
|
9 |
+
def get_nearest_neighbors(word, model, k=5):
|
10 |
+
neighbors = model.get_nearest_neighbors(word, k=k)
|
11 |
+
return neighbors
|
12 |
+
|
13 |
+
|
14 |
+
# Define the K-nearest neighbors function
|
15 |
+
def find_nearest_neighbors(text, k):
|
16 |
+
neighbors = model.get_nearest_neighbors(text, k=k+3)
|
17 |
+
return neighbors[2:]
|
18 |
+
|
19 |
+
|
20 |
+
# Create the Gradio interface
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=find_nearest_neighbors,
|
23 |
+
inputs=[
|
24 |
+
gr.Textbox(lines=2, placeholder="Enter text here...", label="Input Text"),
|
25 |
+
gr.Slider(1, 10, value=5, label="Number of Neighbors")
|
26 |
+
],
|
27 |
+
outputs=gr.JSON(label="Nearest Neighbors"),
|
28 |
+
title="FastText K-Nearest Neighbors Finder",
|
29 |
+
description="Enter a text and the number of neighbors to find the closest matches using the FastText model.",
|
30 |
+
examples=[["king", 5], ["queen", 3]],
|
31 |
+
theme="default",
|
32 |
+
css="style.css"
|
33 |
+
)
|
34 |
+
|
35 |
+
# Launch the app
|
36 |
+
if __name__ == "__main__":
|
37 |
+
iface.launch(share=True)
|