Update app.py
Browse files
app.py
CHANGED
|
@@ -3,49 +3,44 @@ import torch.nn.functional as F
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
# 模型名称
|
| 7 |
model_name = "baidu/ERNIE-4.5-0.3B-PT"
|
| 8 |
|
| 9 |
-
# 加载 tokenizer 和模型(首次运行可能较慢)
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
|
|
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
model_name,
|
| 13 |
trust_remote_code=True,
|
| 14 |
-
device_map="auto",
|
| 15 |
torch_dtype=torch.bfloat16
|
| 16 |
-
)
|
|
|
|
| 17 |
embedding_layer = model.get_input_embeddings()
|
| 18 |
|
| 19 |
-
# 提取句子的平均 embedding
|
| 20 |
def get_sentence_embedding(text):
|
| 21 |
-
inputs = tokenizer(text, return_tensors="pt", add_special_tokens=True)
|
| 22 |
-
input_ids = inputs["input_ids"]
|
| 23 |
with torch.no_grad():
|
| 24 |
-
embeddings = embedding_layer(input_ids)
|
| 25 |
-
sentence_embedding = embeddings.mean(dim=1)
|
| 26 |
return sentence_embedding
|
| 27 |
|
| 28 |
-
# Gradio 回调函数
|
| 29 |
def calculate_similarity(sentence1, sentence2):
|
| 30 |
emb1 = get_sentence_embedding(sentence1)
|
| 31 |
emb2 = get_sentence_embedding(sentence2)
|
| 32 |
similarity = F.cosine_similarity(emb1, emb2).item()
|
| 33 |
return f"Similarity: {similarity:.4f}"
|
| 34 |
|
| 35 |
-
# Gradio 界面
|
| 36 |
-
title = "Calculate two sentences's similarity by ERNIE 4.5-0.3B's embedding layer"
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=calculate_similarity,
|
| 39 |
inputs=[
|
| 40 |
gr.Textbox(label="Sentence 1", placeholder="我爱北京"),
|
| 41 |
-
gr.Textbox(label="Sentence 2", placeholder="我爱上海")
|
| 42 |
],
|
| 43 |
outputs=gr.Textbox(label="Similarity"),
|
| 44 |
-
title=
|
| 45 |
description="This app uses the embedding layer of Baidu ERNIE-4.5-0.3B-PT model to compute the cosine similarity between two sentences.",
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# 启动 Gradio app
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
demo.launch()
|
|
|
|
| 51 |
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
model_name = "baidu/ERNIE-4.5-0.3B-PT"
|
| 7 |
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
+
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
model_name,
|
| 13 |
trust_remote_code=True,
|
|
|
|
| 14 |
torch_dtype=torch.bfloat16
|
| 15 |
+
).to(device)
|
| 16 |
+
|
| 17 |
embedding_layer = model.get_input_embeddings()
|
| 18 |
|
|
|
|
| 19 |
def get_sentence_embedding(text):
|
| 20 |
+
inputs = tokenizer(text, return_tensors="pt", add_special_tokens=True).to(device)
|
|
|
|
| 21 |
with torch.no_grad():
|
| 22 |
+
embeddings = embedding_layer(inputs["input_ids"])
|
| 23 |
+
sentence_embedding = embeddings.mean(dim=1)
|
| 24 |
return sentence_embedding
|
| 25 |
|
|
|
|
| 26 |
def calculate_similarity(sentence1, sentence2):
|
| 27 |
emb1 = get_sentence_embedding(sentence1)
|
| 28 |
emb2 = get_sentence_embedding(sentence2)
|
| 29 |
similarity = F.cosine_similarity(emb1, emb2).item()
|
| 30 |
return f"Similarity: {similarity:.4f}"
|
| 31 |
|
|
|
|
|
|
|
| 32 |
demo = gr.Interface(
|
| 33 |
fn=calculate_similarity,
|
| 34 |
inputs=[
|
| 35 |
gr.Textbox(label="Sentence 1", placeholder="我爱北京"),
|
| 36 |
+
gr.Textbox(label="Sentence 2", placeholder="我爱上海"),
|
| 37 |
],
|
| 38 |
outputs=gr.Textbox(label="Similarity"),
|
| 39 |
+
title="Calculate two sentences's similarity by ERNIE 4.5-0.3B's embedding layer",
|
| 40 |
description="This app uses the embedding layer of Baidu ERNIE-4.5-0.3B-PT model to compute the cosine similarity between two sentences.",
|
| 41 |
)
|
| 42 |
|
|
|
|
| 43 |
if __name__ == "__main__":
|
| 44 |
+
demo.launch(share=True)
|
| 45 |
+
|
| 46 |
|