Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 3 |
|
| 4 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 5 |
|
| 6 |
-
def get_embedding(texts_input
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
embeddings = embedding_model.encode(
|
| 16 |
texts, convert_to_numpy=True, normalize_embeddings=True
|
|
@@ -19,8 +24,8 @@ def get_embedding(texts_input: str):
|
|
| 19 |
|
| 20 |
demo = gr.Interface(
|
| 21 |
fn=get_embedding,
|
| 22 |
-
inputs=gr.
|
| 23 |
-
outputs=gr.JSON(label="Embeddings")
|
| 24 |
)
|
| 25 |
|
| 26 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 6 |
|
| 7 |
+
def get_embedding(texts_input):
|
| 8 |
+
# Parse input nếu là JSON list
|
| 9 |
+
if isinstance(texts_input, str):
|
| 10 |
+
try:
|
| 11 |
+
texts = json.loads(texts_input)
|
| 12 |
+
if not isinstance(texts, list):
|
| 13 |
+
texts = [texts_input]
|
| 14 |
+
except:
|
| 15 |
+
# fallback: mỗi dòng 1 chunk
|
| 16 |
+
texts = [line.strip() for line in texts_input.split("\n") if line.strip()]
|
| 17 |
+
else:
|
| 18 |
+
texts = texts_input # nếu client gửi list thật sự
|
| 19 |
|
| 20 |
embeddings = embedding_model.encode(
|
| 21 |
texts, convert_to_numpy=True, normalize_embeddings=True
|
|
|
|
| 24 |
|
| 25 |
demo = gr.Interface(
|
| 26 |
fn=get_embedding,
|
| 27 |
+
inputs=gr.JSON(label="Input chunks"), # <- quan trọng: JSON input
|
| 28 |
+
outputs=gr.JSON(label="Embeddings")
|
| 29 |
)
|
| 30 |
|
| 31 |
demo.launch()
|