BienKieu commited on
Commit
e75ac30
·
verified ·
1 Parent(s): bac579b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
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: str):
7
- import json
8
- try:
9
- texts = json.loads(texts_input)
10
- if not isinstance(texts, list):
11
- texts = [texts_input]
12
- except:
13
- texts = [t.strip() for t in texts_input.split("\n") if t.strip()]
 
 
 
 
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.Textbox(lines=10, placeholder="JSON list or each line one text"),
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()