File size: 1,826 Bytes
f25a1bf
e74dd14
f25a1bf
 
 
e74dd14
f25a1bf
 
 
 
 
 
 
e74dd14
 
f25a1bf
 
 
 
 
 
 
 
 
 
 
 
e74dd14
f25a1bf
 
 
 
 
 
 
 
 
 
 
 
 
e74dd14
 
f25a1bf
 
 
 
e74dd14
 
 
 
 
 
f25a1bf
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
from sentence_transformers import SentenceTransformer

print("Loading IndoBERT model...")
MODEL_NAME = "indobenchmark/indobert-base-p1"
model = SentenceTransformer(MODEL_NAME)
print("Model loaded!")

def embed_single(text):
    """For Gradio interface - single text"""
    if not text:
        return {"error": "Text required"}
    
    embedding = model.encode(text, normalize_embeddings=True).tolist()
    
    return {
        "success": True,
        "embedding": embedding,
        "dimension": len(embedding)
    }

def embed_batch(texts):
    """For Gradio interface - batch texts"""
    if not texts:
        return {"error": "Texts required"}
    
    text_list = [t.strip() for t in texts.split('\n') if t.strip()]
    embeddings = model.encode(text_list, normalize_embeddings=True).tolist()
    
    return {
        "success": True,
        "embeddings": embeddings,
        "count": len(embeddings),
        "dimension": len(embeddings[0]) if embeddings else 0
    }

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# 🇮🇩 IndoBERT Embedding API")
    
    with gr.Tab("Single"):
        input_single = gr.Textbox(label="Text", lines=3, placeholder="Enter Indonesian text...")
        btn_single = gr.Button("Generate Embedding")
        output_single = gr.JSON(label="Result")
        btn_single.click(embed_single, inputs=input_single, outputs=output_single)
    
    with gr.Tab("Batch"):
        input_batch = gr.Textbox(
            label="Texts (one per line)", 
            lines=10,
            placeholder="Enter multiple Indonesian texts, one per line..."
        )
        btn_batch = gr.Button("Generate Batch Embeddings")
        output_batch = gr.JSON(label="Result")
        btn_batch.click(embed_batch, inputs=input_batch, outputs=output_batch)

demo.launch()