Spaces:
Sleeping
Sleeping
indiejoseph
commited on
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +97 -0
- emb.json +3 -0
- requirements.txt +2 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
emb.json filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import asyncio
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
|
6 |
+
model = SentenceTransformer("hon9kon9ize/bert-large-cantonese")
|
7 |
+
df = None
|
8 |
+
|
9 |
+
|
10 |
+
def get_dataframe():
|
11 |
+
global df
|
12 |
+
|
13 |
+
if df is not None:
|
14 |
+
return df
|
15 |
+
|
16 |
+
df = pd.read_json("emb.json")
|
17 |
+
df = df.drop_duplicates(subset=["artist_name", "track_name"])
|
18 |
+
|
19 |
+
return df
|
20 |
+
|
21 |
+
|
22 |
+
def cosine_similarity(a, b):
|
23 |
+
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
24 |
+
|
25 |
+
|
26 |
+
def find_songs(text):
|
27 |
+
df = get_dataframe()
|
28 |
+
text_embedding = get_embedding(text)
|
29 |
+
df["similarity"] = df["lyrics_embedding"].apply(
|
30 |
+
lambda x: cosine_similarity(x, text_embedding)
|
31 |
+
)
|
32 |
+
df = df.sort_values(by="similarity", ascending=False)
|
33 |
+
top_5 = df.head(5)
|
34 |
+
|
35 |
+
return "### 以下係你嘅歌名推介:\n" + "\n".join(
|
36 |
+
[
|
37 |
+
f"- {row['artist_name']} 嘅 **[「{row['track_name']}」](https://open.spotify.com/track/{row['track_id']})**(相似度:{row['similarity']:.2f})"
|
38 |
+
for _, row in top_5.iterrows()
|
39 |
+
]
|
40 |
+
)
|
41 |
+
|
42 |
+
|
43 |
+
def get_embedding(text):
|
44 |
+
return model.encode(text)
|
45 |
+
|
46 |
+
|
47 |
+
# Create Gradio application
|
48 |
+
import gradio as gr
|
49 |
+
|
50 |
+
|
51 |
+
async def create_demo():
|
52 |
+
example1 = """故事開始於一個悲傷的雨夜,主角站在街頭,淋雨中凝視著遠方。心裡充滿了對一段失去的愛情的痛苦和迷惘。回想起往事,他感到愛情並非他當初所想像的那麼美好,無法找到回到對方身邊的路,更不用說如何忘記過往。
|
53 |
+
|
54 |
+
淚水在眼底打轉,他感到迷失,不知道該往哪裡去,只是心中不斷地呼喚著對方的名字,渴望重新找回失去的愛。他開始思考,是應該安靜地離開這段過往,還是勇敢地留下來,面對愛情的種種無奈。
|
55 |
+
|
56 |
+
在迷惘中,他決定給自己一個機會。或許他應該離開,或者他應該在原地等待,等待對方明白他所付出的愛是永遠不會離開的。這段故事充滿了對愛情的掙扎、遺憾和無奈,卻也帶著一絲希望和堅持。結局如何,只有時間能給予答案。"""
|
57 |
+
example2 = "我唔搵唔返一啲嘢,搵唔返。"
|
58 |
+
example3 = "香港有國安法"
|
59 |
+
example4 = "雞!全部都係雞!"
|
60 |
+
description = """呢個 space 利用咗廣東話 Bert 語言模型,將歌詞轉換成向量,再用 cosine similarity 計算輸入嘅文字同 2394 首粵語歌唧歌詞之間嘅相似度,嚟畀出個歌名推介。
|
61 |
+
|
62 |
+
⚠️ 要註意!呢個唔係關鍵字搜尋,而係用文字嘅意思去比對每首歌嘅歌詞內容,所以有時候會有啲奇怪嘅結果。
|
63 |
+
"""
|
64 |
+
css = """
|
65 |
+
.output {
|
66 |
+
padding: 10px;
|
67 |
+
min-height: 200px;
|
68 |
+
}
|
69 |
+
"""
|
70 |
+
|
71 |
+
demo = gr.Interface(
|
72 |
+
fn=find_songs,
|
73 |
+
css=css,
|
74 |
+
inputs=[
|
75 |
+
gr.Textbox(
|
76 |
+
label="求其打啲嘢去搵歌名",
|
77 |
+
lines=5,
|
78 |
+
placeholder="請輸入歌詞",
|
79 |
+
),
|
80 |
+
],
|
81 |
+
outputs=[
|
82 |
+
gr.Markdown(elem_classes="output"),
|
83 |
+
],
|
84 |
+
examples=[example1, example2, example3, example4],
|
85 |
+
title="粵語流行歌相似度",
|
86 |
+
description=description,
|
87 |
+
analytics_enabled=False,
|
88 |
+
allow_flagging=False,
|
89 |
+
)
|
90 |
+
|
91 |
+
return demo
|
92 |
+
|
93 |
+
|
94 |
+
# Run the application
|
95 |
+
if __name__ == "__main__":
|
96 |
+
demo = asyncio.run(create_demo())
|
97 |
+
demo.launch()
|
emb.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:42d86b95f9d4422c247ac916161fa1a86f8fb886a29414866365b6b5717f49b1
|
3 |
+
size 50336246
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio==4.39.0
|
2 |
+
sentence_transformers==3.0.1
|