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