Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from sentence_transformers import SentenceTransformer | |
| import numpy as np | |
| # Load model once at startup | |
| st_model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2") | |
| TITLE = "# Text → Vector (all-mpnet-base-v2)" | |
| DESC = ( | |
| "Masukkan **kalimat** lalu dapatkan **embedding vector** " | |
| "(opsional dinormalisasi L2). Model: `sentence-transformers/all-mpnet-base-v2`." | |
| ) | |
| def embed(text: str, normalize: bool = True): | |
| text = (text or "").strip() | |
| if not text: | |
| return [], 0 | |
| vec = st_model.encode([text], normalize_embeddings=normalize)[0] | |
| return vec.tolist(), int(vec.shape[0]) | |
| with gr.Blocks() as demo: | |
| gr.Markdown(TITLE) | |
| gr.Markdown(DESC) | |
| with gr.Row(): | |
| text_in = gr.Textbox( | |
| label="Kalimat", | |
| placeholder="Tulis kalimat di sini...", | |
| lines=3, | |
| ) | |
| normalize = gr.Checkbox(value=True, label="Normalize embedding (L2)") | |
| btn = gr.Button("Compute Embedding", variant="primary") | |
| with gr.Row(): | |
| vec_out = gr.JSON(label="Vector (list of floats)") | |
| dim_out = gr.Number(label="Dimensi vektor", interactive=False) | |
| gr.Examples( | |
| examples=[ | |
| ["Halo dunia!"], | |
| ["Machine learning is fun."], | |
| ["Saya sedang membangun demo embedding sederhana."], | |
| ], | |
| inputs=[text_in], | |
| label="Contoh", | |
| ) | |
| btn.click(embed, inputs=[text_in, normalize], outputs=[vec_out, dim_out]) | |
| # Enable queue for concurrency | |
| demo.queue() | |
| if __name__ == "__main__": | |
| demo.launch() | |