Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
+
from sklearn.decomposition import PCA
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import torch
|
| 7 |
+
import torchvision
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import io
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def similarity_score(text1, text2):
|
| 15 |
+
# Load PhoBERT tokenizer and model
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
|
| 17 |
+
model = AutoModel.from_pretrained("vinai/phobert-base", output_hidden_states=True)
|
| 18 |
+
|
| 19 |
+
# Encode the two texts using PhoBERT tokenizer
|
| 20 |
+
tokens1 = tokenizer(text1, return_tensors='pt', padding=True)
|
| 21 |
+
tokens2 = tokenizer(text2, return_tensors='pt', padding=True)
|
| 22 |
+
# Pass the encoded tokens to PhoBERT to get the hidden states
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
hidden_states1 = model(tokens1['input_ids'], tokens1['attention_mask'])
|
| 25 |
+
hidden_states2 = model(tokens2['input_ids'], tokens2['attention_mask'])
|
| 26 |
+
|
| 27 |
+
# Calculate cosine similarity between the last hidden states of the two texts
|
| 28 |
+
similarity = cosine_similarity(hidden_states1[0][:, -1, :], hidden_states2[0][:, -1, :])[0][0]
|
| 29 |
+
#return similarity
|
| 30 |
+
pca = PCA(n_components=2)
|
| 31 |
+
pca.fit_transform(torch.cat((hidden_states1[0][:, -1, :], hidden_states2[0][:, -1, :])))
|
| 32 |
+
|
| 33 |
+
# tạo biểu đồ
|
| 34 |
+
fig, ax = plt.subplots()
|
| 35 |
+
ax.scatter(pca.components_[0][0], pca.components_[1][0], color='red')
|
| 36 |
+
ax.text(pca.components_[0][0], pca.components_[1][0], text1, fontsize=12)
|
| 37 |
+
ax.scatter(pca.components_[0][1], pca.components_[1][1], color='blue')
|
| 38 |
+
ax.text(pca.components_[0][1], pca.components_[1][1], text2, fontsize=12)
|
| 39 |
+
ax.set_title(f"Khoảng các Consin của 2 câu: {similarity:.4f}")
|
| 40 |
+
|
| 41 |
+
# chuyển đổi hình ảnh matplotlib sang định dạng bytes và hiển thị trên Gradio
|
| 42 |
+
buffer = io.BytesIO()
|
| 43 |
+
fig.canvas.print_png(buffer)
|
| 44 |
+
img_bytes = buffer.getvalue()
|
| 45 |
+
return Image.open(io.BytesIO(img_bytes))
|
| 46 |
+
|
| 47 |
+
input_text1 = gr.inputs.Textbox(label="Câu 1")
|
| 48 |
+
input_text2 = gr.inputs.Textbox(label="Câu 2")
|
| 49 |
+
output_score = gr.outputs.Textbox(label="Điểm tương đồng")
|
| 50 |
+
|
| 51 |
+
app = gr.Interface(
|
| 52 |
+
fn=similarity_score,
|
| 53 |
+
inputs=[input_text1, input_text2],
|
| 54 |
+
outputs=gr.outputs.Image(type='numpy'),
|
| 55 |
+
title="So sánh độ giống nhau giữa 2 câu",
|
| 56 |
+
description="Nhập vào 2 câu, kết quả trả ra độ tương đồng của 2 câu đó",
|
| 57 |
+
allow_flagging=False,
|
| 58 |
+
layout="vertical",
|
| 59 |
+
theme="default",)
|
| 60 |
+
app.launch(share=True)
|