Spaces:
Sleeping
Sleeping
feat: update
Browse files
app.py
CHANGED
|
@@ -5,22 +5,32 @@ from sentence_transformers import SentenceTransformer
|
|
| 5 |
model = SentenceTransformer(model_name_or_path='Alibaba-NLP/gte-multilingual-base',
|
| 6 |
trust_remote_code=True)
|
| 7 |
|
| 8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
# Mã hóa các câu
|
| 11 |
embeddings = model.encode(sentences)
|
| 12 |
-
return embeddings.tolist() # Chuyển
|
| 13 |
except Exception as e:
|
| 14 |
return f"Error: {str(e)}"
|
| 15 |
|
| 16 |
# Tạo giao diện Gradio
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Khởi chạy giao diện
|
| 26 |
demo.launch()
|
|
|
|
| 5 |
model = SentenceTransformer(model_name_or_path='Alibaba-NLP/gte-multilingual-base',
|
| 6 |
trust_remote_code=True)
|
| 7 |
|
| 8 |
+
def add_sentence(sentences, new_sentence):
|
| 9 |
+
sentences.append(new_sentence)
|
| 10 |
+
return sentences, "", f"Danh sách câu: {sentences}"
|
| 11 |
+
|
| 12 |
+
def gte_model(sentences):
|
| 13 |
try:
|
|
|
|
| 14 |
embeddings = model.encode(sentences)
|
| 15 |
+
return embeddings.tolist() # Chuyển numpy array sang danh sách
|
| 16 |
except Exception as e:
|
| 17 |
return f"Error: {str(e)}"
|
| 18 |
|
| 19 |
# Tạo giao diện Gradio
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("# Mô hình GTE Multilingual")
|
| 22 |
+
gr.Markdown("Nhập từng câu, sau đó nhấn 'Thêm câu' để thêm vào danh sách. Nhấn 'Mã hóa' để nhận kết quả.")
|
| 23 |
+
|
| 24 |
+
sentence_input = gr.Textbox(label="Nhập câu", placeholder="Nhập một câu tại đây...")
|
| 25 |
+
add_button = gr.Button("Thêm câu")
|
| 26 |
+
sentences_state = gr.State([]) # Lưu trữ danh sách các câu
|
| 27 |
+
sentence_list_display = gr.Markdown("Danh sách câu: []")
|
| 28 |
+
encode_button = gr.Button("Mã hóa")
|
| 29 |
+
output = gr.JSON(label="Kết quả mã hóa")
|
| 30 |
+
|
| 31 |
+
# Liên kết các sự kiện và hàm
|
| 32 |
+
add_button.click(add_sentence, inputs=[sentences_state, sentence_input], outputs=[sentences_state, sentence_input, sentence_list_display])
|
| 33 |
+
encode_button.click(gte_model, inputs=sentences_state, outputs=output)
|
| 34 |
|
| 35 |
# Khởi chạy giao diện
|
| 36 |
demo.launch()
|