Update app.py
Browse files
app.py
CHANGED
|
@@ -45,12 +45,11 @@ def save_tags_to_cloud(tags_data):
|
|
| 45 |
print(f"同步至雲端失敗。原因: {e}")
|
| 46 |
|
| 47 |
|
| 48 |
-
def
|
| 49 |
-
# ✨ 修正點 1:改為直接從雲端 Dataset 讀取最新的標籤庫
|
| 50 |
current_tags = load_tags_from_cloud()
|
| 51 |
-
|
| 52 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 53 |
|
|
|
|
| 54 |
prompt = f"""
|
| 55 |
你是一位專業的音樂與情感分析師。
|
| 56 |
請針對用戶提供的【歌曲:{song}】與【歌手:{artist}】,利用聯網搜尋功能找到這首歌的完整背景、歌詞與音樂資訊。
|
|
@@ -63,21 +62,25 @@ def analyze_song_v5(artist, song):
|
|
| 63 |
### 🏷️ 2. 統一標籤庫
|
| 64 |
[請將這首歌的所有相關標籤,全部統一用「英文逗號 ,」隔開,排成一整行輸出,不要分段。]
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
字串中必須包含以下兩大類標籤:
|
| 67 |
-
1. 靜態標籤(
|
| 68 |
-
專輯或歌曲衍生名、發行年份、年代標籤(如
|
| 69 |
|
| 70 |
2. 動態標籤(必須對照下方的【標準資料庫】進行語意對齊):
|
| 71 |
包含【情緒基調】、【視覺畫面】、【適用場景】、【風格曲風】。
|
| 72 |
-
- 語意對齊:如果你想到的詞與下方資料庫中的詞高度相似,必須自動校正並採用資料庫中的詞。
|
| 73 |
-
- 發現新詞規則:如果你認為現有資料庫無法涵蓋,允許你創造新詞,但必須在
|
| 74 |
-
(例如:創造新曲風寫成 `[NEW:風格曲風]
|
| 75 |
|
| 76 |
-
【目前的標準資料庫】(供你對齊
|
| 77 |
{json.dumps(current_tags, ensure_ascii=False)}
|
| 78 |
|
| 79 |
-
正確的【統一標籤庫】輸出範例(必須只有一行長字串):
|
| 80 |
-
|
| 81 |
"""
|
| 82 |
|
| 83 |
response = client.models.generate_content(
|
|
@@ -90,35 +93,36 @@ def analyze_song_v5(artist, song):
|
|
| 90 |
|
| 91 |
result_text = response.text
|
| 92 |
|
|
|
|
| 93 |
new_found_tags = re.findall(r'\[NEW:(情緒基調|視覺畫面|適用場景|風格曲風)\]([^,\n]+)', result_text)
|
| 94 |
|
| 95 |
has_updates = False
|
| 96 |
for category, tag in new_found_tags:
|
| 97 |
tag = tag.strip()
|
|
|
|
| 98 |
if tag not in current_tags[category]:
|
| 99 |
current_tags[category].append(tag)
|
| 100 |
has_updates = True
|
| 101 |
|
| 102 |
if has_updates:
|
| 103 |
-
# ✨ 修正點 2:發現新詞時,改為調用雲端儲存函式
|
| 104 |
save_tags_to_cloud(current_tags)
|
| 105 |
|
|
|
|
| 106 |
clean_output = re.sub(r'\[NEW:[^\]]+\]', '', result_text)
|
| 107 |
return clean_output
|
| 108 |
|
| 109 |
|
| 110 |
-
#
|
| 111 |
demo = gr.Interface(
|
| 112 |
-
fn=
|
| 113 |
inputs=[
|
| 114 |
-
gr.Textbox(label="歌手 / 藝術家", placeholder="例如:
|
| 115 |
-
gr.Textbox(label="歌曲名稱", placeholder="例如:
|
| 116 |
],
|
| 117 |
-
outputs=gr.Markdown(label="AI
|
| 118 |
-
title="🎵 智慧音樂關鍵字與意境分析系統",
|
| 119 |
-
description="輸入歌手與歌名,AI 將自動聯網檢索,生成包含年代、曲風、BPM、金句等一體化標籤庫,
|
| 120 |
)
|
| 121 |
|
| 122 |
-
# 啟動 Gradio 服務
|
| 123 |
if __name__ == "__main__":
|
| 124 |
demo.launch()
|
|
|
|
| 45 |
print(f"同步至雲端失敗。原因: {e}")
|
| 46 |
|
| 47 |
|
| 48 |
+
def analyze_song_v6(artist, song):
|
|
|
|
| 49 |
current_tags = load_tags_from_cloud()
|
|
|
|
| 50 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 51 |
|
| 52 |
+
# 核心 Prompt:命令 AI 在輸出一體化標籤時,自動進行中英雙語配對
|
| 53 |
prompt = f"""
|
| 54 |
你是一位專業的音樂與情感分析師。
|
| 55 |
請針對用戶提供的【歌曲:{song}】與【歌手:{artist}】,利用聯網搜尋功能找到這首歌的完整背景、歌詞與音樂資訊。
|
|
|
|
| 62 |
### 🏷️ 2. 統一標籤庫
|
| 63 |
[請將這首歌的所有相關標籤,全部統一用「英文逗號 ,」隔開,排成一整行輸出,不要分段。]
|
| 64 |
|
| 65 |
+
✨【中英雙語對照輸出規則】✨:
|
| 66 |
+
1. 只要輸出了「中文/非英文」的標籤,其後方必須「緊接著」輸出它的英文翻譯,並用逗號隔開。
|
| 67 |
+
2. 如果標籤本身就是數字(如發行年份 2007、速度 135 BPM)或是原本就屬於英文單字(如 Taiwan, 2000s),則保持原樣即可,絕對不要重複翻譯。
|
| 68 |
+
|
| 69 |
字串中必須包含以下兩大類標籤:
|
| 70 |
+
1. 靜態標籤(需遵循雙語對照規則):
|
| 71 |
+
歌曲名、專輯或歌曲衍生名、發行年份、年代標籤(如 2000年代, 2000s)、地區、相關影視作品名稱、最著名的副歌第一句歌詞、歌曲速度(如 135 BPM)。
|
| 72 |
|
| 73 |
2. 動態標籤(必須對照下方的【標準資料庫】進行語意對齊):
|
| 74 |
包含【情緒基調】、【視覺畫面】、【適用場景】、【風格曲風】。
|
| 75 |
+
- 語意對齊:如果你想到的詞與下方資料庫中的詞高度相似,必須自動校正並採用資料庫中的詞(採用後一樣要在後面補上英文翻譯)。
|
| 76 |
+
- 發現新詞規則:如果你認為現有資料庫無法涵蓋,允許你創造新詞,但必須在「中文新詞」前面加上 `[NEW:類別]` 標記,並在其後方照常輸出英文翻譯。
|
| 77 |
+
(例如:創造新曲風寫成 `[NEW:風格曲風]周氏情歌, Chou Style`;創造新情感寫成 `[NEW:情緒基調]青澀回憶, Youthful Memories`)。
|
| 78 |
|
| 79 |
+
【目前的標準資料庫】(供你對齊與參考語意):
|
| 80 |
{json.dumps(current_tags, ensure_ascii=False)}
|
| 81 |
|
| 82 |
+
正確的【統一標籤庫】中英交錯輸出範例(必須只有一行長字串,不要帶引號):
|
| 83 |
+
蒲公英的約定, The Promised Dandelion, 我很忙, On the Run, 2007, 2000年代, 2000s, 台灣, Taiwan, 不能說的秘密, Secret, 一起長大的約定 那樣清晰 打過勾的我相信, The promise of growing up together is so clear, 135 BPM, [NEW:風格曲風]周氏情歌, Chou Style, 溫柔療癒, Gentle and Healing, 璀璨星空, Starry Night, 深夜沉思, Midnight Reflection
|
| 84 |
"""
|
| 85 |
|
| 86 |
response = client.models.generate_content(
|
|
|
|
| 93 |
|
| 94 |
result_text = response.text
|
| 95 |
|
| 96 |
+
# 利用正則表達式,精準抓出帶有 [NEW:類別] 的中文新詞(遇到逗號就會停,所以不會抓到後面的英文翻譯)
|
| 97 |
new_found_tags = re.findall(r'\[NEW:(情緒基調|視覺畫面|適用場景|風格曲風)\]([^,\n]+)', result_text)
|
| 98 |
|
| 99 |
has_updates = False
|
| 100 |
for category, tag in new_found_tags:
|
| 101 |
tag = tag.strip()
|
| 102 |
+
# 確保這個中文新詞不在該類別的 JSON 清單中,然後塞進去
|
| 103 |
if tag not in current_tags[category]:
|
| 104 |
current_tags[category].append(tag)
|
| 105 |
has_updates = True
|
| 106 |
|
| 107 |
if has_updates:
|
|
|
|
| 108 |
save_tags_to_cloud(current_tags)
|
| 109 |
|
| 110 |
+
# 用正則把所有的 `[NEW:XXXXX]` 標記通通抹除乾淨,回傳完美的成果給使用者
|
| 111 |
clean_output = re.sub(r'\[NEW:[^\]]+\]', '', result_text)
|
| 112 |
return clean_output
|
| 113 |
|
| 114 |
|
| 115 |
+
# 建立 Gradio 網頁介面
|
| 116 |
demo = gr.Interface(
|
| 117 |
+
fn=analyze_song_v6,
|
| 118 |
inputs=[
|
| 119 |
+
gr.Textbox(label="歌手 / 藝術家", placeholder="例如:周杰倫"),
|
| 120 |
+
gr.Textbox(label="歌曲名稱", placeholder="例如:蒲公英的約定")
|
| 121 |
],
|
| 122 |
+
outputs=gr.Markdown(label="AI 雙語音樂關鍵字解析"),
|
| 123 |
+
title="🎵 智慧音樂關鍵字與意境分析系統 (中英雙語版)",
|
| 124 |
+
description="輸入歌手與歌名,AI 將自動聯網檢索,生成包含年代、曲風、BPM、金句等一體化標籤庫,所有中文標籤皆會自動附帶英文翻譯。"
|
| 125 |
)
|
| 126 |
|
|
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch()
|