Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app (4).py +54 -0
- requirements (4).txt +4 -0
app (4).py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load model once at startup
|
| 6 |
+
st_model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
|
| 7 |
+
|
| 8 |
+
TITLE = "# Text → Vector (all-mpnet-base-v2)"
|
| 9 |
+
DESC = (
|
| 10 |
+
"Masukkan **kalimat** lalu dapatkan **embedding vector** "
|
| 11 |
+
"(opsional dinormalisasi L2). Model: `sentence-transformers/all-mpnet-base-v2`."
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def embed(text: str, normalize: bool = True):
|
| 15 |
+
text = (text or "").strip()
|
| 16 |
+
if not text:
|
| 17 |
+
return [], 0
|
| 18 |
+
vec = st_model.encode([text], normalize_embeddings=normalize)[0]
|
| 19 |
+
return vec.tolist(), int(vec.shape[0])
|
| 20 |
+
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown(TITLE)
|
| 23 |
+
gr.Markdown(DESC)
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
text_in = gr.Textbox(
|
| 27 |
+
label="Kalimat",
|
| 28 |
+
placeholder="Tulis kalimat di sini...",
|
| 29 |
+
lines=3,
|
| 30 |
+
)
|
| 31 |
+
normalize = gr.Checkbox(value=True, label="Normalize embedding (L2)")
|
| 32 |
+
btn = gr.Button("Compute Embedding", variant="primary")
|
| 33 |
+
|
| 34 |
+
with gr.Row():
|
| 35 |
+
vec_out = gr.JSON(label="Vector (list of floats)")
|
| 36 |
+
dim_out = gr.Number(label="Dimensi vektor", interactive=False)
|
| 37 |
+
|
| 38 |
+
gr.Examples(
|
| 39 |
+
examples=[
|
| 40 |
+
["Halo dunia!"],
|
| 41 |
+
["Machine learning is fun."],
|
| 42 |
+
["Saya sedang membangun demo embedding sederhana."],
|
| 43 |
+
],
|
| 44 |
+
inputs=[text_in],
|
| 45 |
+
label="Contoh",
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
btn.click(embed, inputs=[text_in, normalize], outputs=[vec_out, dim_out])
|
| 49 |
+
|
| 50 |
+
# Enable queue for concurrency
|
| 51 |
+
demo.queue()
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
requirements (4).txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.31.4
|
| 2 |
+
sentence-transformers>=2.6.1
|
| 3 |
+
torch>=2.1.0
|
| 4 |
+
numpy>=1.26
|