Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from sentence_transformers import SentenceTransformer | |
| import json | |
| embedding_model = SentenceTransformer("all-MiniLM-L6-v2") | |
| def get_embedding(texts_input): | |
| # Parse input nếu là JSON list | |
| if isinstance(texts_input, str): | |
| try: | |
| texts = json.loads(texts_input) | |
| if not isinstance(texts, list): | |
| texts = [texts_input] | |
| except: | |
| # fallback: mỗi dòng 1 chunk | |
| texts = [line.strip() for line in texts_input.split("\n") if line.strip()] | |
| else: | |
| texts = texts_input # nếu client gửi list thật sự | |
| embeddings = embedding_model.encode( | |
| texts, convert_to_numpy=True, normalize_embeddings=True | |
| ) | |
| return embeddings.tolist() | |
| demo = gr.Interface( | |
| fn=get_embedding, | |
| inputs=gr.JSON(label="Input chunks"), # <- quan trọng: JSON input | |
| outputs=gr.JSON(label="Embeddings") | |
| ) | |
| demo.launch() |