Update app.py
Browse files
app.py
CHANGED
|
@@ -45,7 +45,7 @@ def save_tags_to_cloud(tags_data):
|
|
| 45 |
print(f"同步至雲端失敗。原因: {e}")
|
| 46 |
|
| 47 |
|
| 48 |
-
def
|
| 49 |
# 📊 階段 1:連線雲端
|
| 50 |
progress(0.1, desc="🔄 正在連線 Hugging Face 雲端下載歷史標籤庫...")
|
| 51 |
current_tags = load_tags_from_cloud()
|
|
@@ -77,7 +77,7 @@ def analyze_song_v7(artist, song, progress=gr.Progress()):
|
|
| 77 |
2. 動態標籤(必須對照下方的【標準資料庫】進行語意對齊):
|
| 78 |
包含【情緒基調】、【視覺畫面】、【適用場景】、【風格曲風】。
|
| 79 |
- 語意對齊:如果你想到的詞與下方資料庫中的詞高度相似,必須自動校正並採用資料庫中的詞(採用後一樣要在後面補上英文翻譯)。
|
| 80 |
-
- 發現新詞規則:如果你認為現有資料庫無法涵蓋,允許你創造新詞,
|
| 81 |
(例如:創造新曲風寫成 `[NEW:風格曲風]周氏情歌, Chou Style`;創造新情感寫成 `[NEW:情緒基調]青澀回憶, Youthful Memories`)。
|
| 82 |
|
| 83 |
【目前的標準資料庫】(供你對齊與參考語意):
|
|
@@ -113,43 +113,56 @@ def analyze_song_v7(artist, song, progress=gr.Progress()):
|
|
| 113 |
progress(0.95, desc="✨ 雲端同步完成!正在進行最後的中英雙語清洗與格式化...")
|
| 114 |
clean_output = re.sub(r'\[NEW:[^\]]+\]', '', result_text)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
progress(1.0, desc="🎉 大功告成!")
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
-
#
|
| 121 |
with gr.Blocks(title="🎵 智慧音樂關鍵字與意境分析系統") as demo:
|
| 122 |
gr.Markdown("# 🎵 智慧音樂關鍵字與意境分析系統 (中英雙語版)")
|
| 123 |
-
gr.Markdown("輸入歌手與歌名,
|
| 124 |
|
| 125 |
-
# 建立左右並排的兩欄佈局(完全複製你截圖中的完美外觀)
|
| 126 |
with gr.Row():
|
| 127 |
# 左半邊:輸入與控制區
|
| 128 |
with gr.Column():
|
| 129 |
artist_input = gr.Textbox(label="歌手 / 藝術家", placeholder="例如:周杰倫")
|
| 130 |
song_input = gr.Textbox(label="歌曲名稱", placeholder="例如:蒲公英的約定")
|
| 131 |
|
| 132 |
-
# 按鈕水平並排
|
| 133 |
with gr.Row():
|
| 134 |
clear_btn = gr.Button("Clear")
|
| 135 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 136 |
|
| 137 |
-
# 右半邊:
|
| 138 |
with gr.Column():
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
# 綁定 Submit 按鈕事件(
|
| 142 |
submit_btn.click(
|
| 143 |
-
fn=
|
| 144 |
inputs=[artist_input, song_input],
|
| 145 |
-
outputs=
|
| 146 |
)
|
| 147 |
|
| 148 |
-
# 綁定 Clear 按鈕事件,一鍵清空
|
| 149 |
clear_btn.click(
|
| 150 |
-
fn=lambda: ("", "", ""),
|
| 151 |
inputs=None,
|
| 152 |
-
outputs=[artist_input, song_input,
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
|
|
|
| 45 |
print(f"同步至雲端失敗。原因: {e}")
|
| 46 |
|
| 47 |
|
| 48 |
+
def analyze_song_v8(artist, song, progress=gr.Progress()):
|
| 49 |
# 📊 階段 1:連線雲端
|
| 50 |
progress(0.1, desc="🔄 正在連線 Hugging Face 雲端下載歷史標籤庫...")
|
| 51 |
current_tags = load_tags_from_cloud()
|
|
|
|
| 77 |
2. 動態標籤(必須對照下方的【標準資料庫】進行語意對齊):
|
| 78 |
包含【情緒基調】、【視覺畫面】、【適用場景】、【風格曲風】。
|
| 79 |
- 語意對齊:如果你想到的詞與下方資料庫中的詞高度相似,必須自動校正並採用資料庫中的詞(採用後一樣要在後面補上英文翻譯)。
|
| 80 |
+
- 發現新詞規則:如果你認為現有資料庫無法涵蓋,允許你創造新詞,幕前必須在「中文新詞」前面加上 `[NEW:類別]` 標記,並在其後方照常輸出英文翻譯。
|
| 81 |
(例如:創造新曲風寫成 `[NEW:風格曲風]周氏情歌, Chou Style`;創造新情感寫成 `[NEW:情緒基調]青澀回憶, Youthful Memories`)。
|
| 82 |
|
| 83 |
【目前的標準資料庫】(供你對齊與參考語意):
|
|
|
|
| 113 |
progress(0.95, desc="✨ 雲端同步完成!正在進行最後的中英雙語清洗與格式化...")
|
| 114 |
clean_output = re.sub(r'\[NEW:[^\]]+\]', '', result_text)
|
| 115 |
|
| 116 |
+
# ✨ 新增優化:利用正則表達式在後台把「情境描述」與「標籤庫」攔截並拆開
|
| 117 |
+
desc_match = re.search(r'### 📝 1\. 意境與情境描述\s*([\s\S]*?)(?=### 🏷️ 2\. 統一標籤庫|$)', clean_output)
|
| 118 |
+
tags_match = re.search(r'### 🏷️ 2\. 統一標籤庫\s*([\s\S]*)', clean_output)
|
| 119 |
+
|
| 120 |
+
# 萃取乾淨的內容
|
| 121 |
+
desc_content = desc_match.group(1).strip() if desc_match else "(未能成功生成情境描述)"
|
| 122 |
+
tags_content = tags_match.group(1).strip() if tags_match else clean_output
|
| 123 |
+
|
| 124 |
progress(1.0, desc="🎉 大功告成!")
|
| 125 |
+
|
| 126 |
+
# 同時回傳兩個結果給前端不同的組件
|
| 127 |
+
return tags_content, desc_content
|
| 128 |
|
| 129 |
|
| 130 |
+
# 使用 gr.Blocks 自訂極簡版面
|
| 131 |
with gr.Blocks(title="🎵 智慧音樂關鍵字與意境分析系統") as demo:
|
| 132 |
gr.Markdown("# 🎵 智慧音樂關鍵字與意境分析系統 (中英雙語版)")
|
| 133 |
+
gr.Markdown("輸入歌手與歌名,AI 自動聯網檢索。畫面上將直接顯示雙語標籤海,情境描述已自動隱藏至下方選單中。")
|
| 134 |
|
|
|
|
| 135 |
with gr.Row():
|
| 136 |
# 左半邊:輸入與控制區
|
| 137 |
with gr.Column():
|
| 138 |
artist_input = gr.Textbox(label="歌手 / 藝術家", placeholder="例如:周杰倫")
|
| 139 |
song_input = gr.Textbox(label="歌曲名稱", placeholder="例如:蒲公英的約定")
|
| 140 |
|
|
|
|
| 141 |
with gr.Row():
|
| 142 |
clear_btn = gr.Button("Clear")
|
| 143 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 144 |
|
| 145 |
+
# 右半邊:輸出區
|
| 146 |
with gr.Column():
|
| 147 |
+
gr.Markdown("### 🏷️ 智慧生成雙語標籤庫")
|
| 148 |
+
output_tags = gr.Markdown() # 這裡會直接大大的顯示標籤
|
| 149 |
+
|
| 150 |
+
# ✨ 關鍵改造:建立一個預設關閉 (open=False) 的摺疊區塊,把描述藏在裡面
|
| 151 |
+
with gr.Accordion("📝 點擊展開:查看 AI 歌曲意境與情境描述", open=False):
|
| 152 |
+
output_desc = gr.Markdown() # 隱藏的情境描述放在這
|
| 153 |
|
| 154 |
+
# 綁定 Submit 按鈕事件(負責將兩個輸出分流塞入對應位置)
|
| 155 |
submit_btn.click(
|
| 156 |
+
fn=analyze_song_v8,
|
| 157 |
inputs=[artist_input, song_input],
|
| 158 |
+
outputs=[output_tags, output_desc]
|
| 159 |
)
|
| 160 |
|
| 161 |
+
# 綁定 Clear 按鈕事件,一鍵清空所有欄位
|
| 162 |
clear_btn.click(
|
| 163 |
+
fn=lambda: ("", "", "", ""),
|
| 164 |
inputs=None,
|
| 165 |
+
outputs=[artist_input, song_input, output_tags, output_desc]
|
| 166 |
)
|
| 167 |
|
| 168 |
if __name__ == "__main__":
|