xxxfortest commited on
Commit
7342b32
1 Parent(s): edb49b9
Files changed (6) hide show
  1. 1_upload.sh +6 -1
  2. app.py +47 -52
  3. example.wav +0 -0
  4. requirements.txt +1 -1
  5. 停泊/app.py +148 -0
  6. 停泊/requirements_bk.txt +6 -0
1_upload.sh CHANGED
@@ -1,3 +1,8 @@
 
 
1
  git add .
2
  git commit -m "Add application file"
3
- git push
 
 
 
 
1
+ git config --global http.proxy http://127.0.0.1:7890
2
+ git config --global https.proxy https://127.0.0.1:7890
3
  git add .
4
  git commit -m "Add application file"
5
+ git push
6
+
7
+ #git remote set-url origin https://huggingface.co/spaces/M4869/WavMark
8
+
app.py CHANGED
@@ -12,8 +12,8 @@ from huggingface_hub import hf_hub_download, HfApi
12
  from wavmark.utils import file_reader
13
 
14
 
15
- # Function to add watermark to audio
16
  def add_watermark(audio_path, watermark_text):
 
17
  assert len(watermark_text) == 16
18
  watermark_npy = np.array([int(i) for i in watermark_text])
19
  # todo: 控制时间
@@ -24,83 +24,74 @@ def add_watermark(audio_path, watermark_text):
24
  tmp_file_name = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + "_" + str(uuid.uuid4()) + ".wav"
25
  tmp_file_path = '/tmp/' + tmp_file_name
26
  soundfile.write(tmp_file_path, watermarked_signal, sr)
27
- return tmp_file_path
 
28
 
29
 
30
- # Function to decode watermark from audio
31
  def decode_watermark(audio_path):
32
- watermarked_signal, sr, audio_length_second = file_reader.read_as_single_channel_16k(audio_path, 16000)
 
 
 
 
 
 
 
33
  payload_decoded, _ = wavmark.decode_watermark(model, watermarked_signal, show_progress=False)
 
34
 
35
  if payload_decoded is None:
36
- return "No Watermark"
37
-
38
- return payload_decoded
39
 
 
 
 
40
 
41
- # Main web app
42
- def main():
43
- max_upload_size = 20 * 1024 * 1024 # 20 MB in bytes
44
 
 
45
  if "def_value" not in st.session_state:
46
  def_val_npy = np.random.choice([0, 1], size=32 - len_start_bit)
47
  def_val_str = "".join([str(i) for i in def_val_npy])
48
  st.session_state.def_value = def_val_str
49
 
50
- st.title("Neural Audio Watermark")
51
- st.write("Choose the action you want to perform:")
52
 
53
- action = st.selectbox("Select Action", ["Add Watermark", "Decode Watermark"])
 
 
 
 
 
 
 
54
 
55
- if action == "Add Watermark":
56
- audio_file = st.file_uploader("Upload Audio File (WAV)", type=["wav"], accept_multiple_files=False,
57
- max_upload_size=max_upload_size)
58
- if audio_file:
59
- tmp_input_audio_file = os.path.join("/tmp/", audio_file.name)
60
- with open(tmp_input_audio_file, "wb") as f:
61
- f.write(audio_file.getbuffer())
62
- st.audio(tmp_input_audio_file, format="audio/wav")
63
 
64
- watermark_text = st.text_input("Enter Watermark", value=st.session_state.def_value)
 
65
 
 
 
 
 
66
  add_watermark_button = st.button("Add Watermark", key="add_watermark_btn")
67
  if add_watermark_button: # 点击按钮后执行的
68
  if audio_file and watermark_text:
69
  with st.spinner("Adding Watermark..."):
70
- # add_watermark_button.empty()
71
- # st.button("Add Watermark", disabled=True)
72
- # st.button("Add Watermark", disabled=True, key="add_watermark_btn_disabled")
73
- t1 = time.time()
74
-
75
- watermarked_audio = add_watermark(tmp_input_audio_file, watermark_text)
76
- encode_time_cost = time.time() - t1
77
-
78
  st.write("Watermarked Audio:")
 
79
  st.audio(watermarked_audio, format="audio/wav")
80
- st.write("Time Cost:%d seconds" % encode_time_cost)
81
 
82
  # st.button("Add Watermark", disabled=False)
83
-
84
- elif action == "Decode Watermark":
85
- audio_file = st.file_uploader("Upload Audio File (WAV/MP3)", type=["wav", "mp3"], accept_multiple_files=False,
86
- max_upload_size=max_upload_size)
87
- if audio_file:
88
- if st.button("Decode Watermark"):
89
- # 1.保存
90
- tmp_file_for_decode_path = os.path.join("/tmp/", audio_file.name)
91
- with open(tmp_file_for_decode_path, "wb") as f:
92
- f.write(audio_file.getbuffer())
93
-
94
- # 2.执行
95
  with st.spinner("Decoding..."):
96
- t1 = time.time()
97
- decoded_watermark = decode_watermark(tmp_file_for_decode_path)
98
- decode_cost = time.time() - t1
99
-
100
- print("decoded_watermark", decoded_watermark)
101
- # Display the decoded watermark
102
- st.write("Decoded Watermark:", decoded_watermark)
103
- st.write("Time Cost:%d seconds" % (decode_cost))
104
 
105
 
106
  if __name__ == "__main__":
@@ -108,5 +99,9 @@ if __name__ == "__main__":
108
 
109
  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
110
  model = wavmark.load_model().to(device)
111
-
112
  main()
 
 
 
 
 
 
12
  from wavmark.utils import file_reader
13
 
14
 
 
15
  def add_watermark(audio_path, watermark_text):
16
+ t1 = time.time()
17
  assert len(watermark_text) == 16
18
  watermark_npy = np.array([int(i) for i in watermark_text])
19
  # todo: 控制时间
 
24
  tmp_file_name = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + "_" + str(uuid.uuid4()) + ".wav"
25
  tmp_file_path = '/tmp/' + tmp_file_name
26
  soundfile.write(tmp_file_path, watermarked_signal, sr)
27
+ encode_time_cost = time.time() - t1
28
+ return tmp_file_path, encode_time_cost
29
 
30
 
 
31
  def decode_watermark(audio_path):
32
+ assert os.path.exists(audio_path)
33
+
34
+ t1 = time.time()
35
+ watermarked_signal, _, audio_length_second = file_reader.read_as_single_channel_16k(audio_path, 16000)
36
+ max_second = 5
37
+ if audio_length_second > max_second:
38
+ watermarked_signal = watermarked_signal[0:16000 * max_second]
39
+
40
  payload_decoded, _ = wavmark.decode_watermark(model, watermarked_signal, show_progress=False)
41
+ decode_cost = time.time() - t1
42
 
43
  if payload_decoded is None:
44
+ return "No Watermark", decode_cost
 
 
45
 
46
+ payload_decoded_str = "".join([str(i) for i in payload_decoded])
47
+ st.write("Result:", payload_decoded_str)
48
+ st.write("Time Cost:%d seconds" % (decode_cost))
49
 
 
 
 
50
 
51
+ def create_default_value():
52
  if "def_value" not in st.session_state:
53
  def_val_npy = np.random.choice([0, 1], size=32 - len_start_bit)
54
  def_val_str = "".join([str(i) for i in def_val_npy])
55
  st.session_state.def_value = def_val_str
56
 
 
 
57
 
58
+ # Main web app
59
+ def main():
60
+ create_default_value()
61
+
62
+ st.title("WavMark")
63
+ st.write("https://github.com/wavmark/wavmark")
64
+
65
+ audio_file = st.file_uploader("Upload Audio", type=["wav", "mp3"], accept_multiple_files=False)
66
 
67
+ if audio_file:
68
+ # 保存文件到本地:
69
+ tmp_input_audio_file = os.path.join("/tmp/", audio_file.name)
70
+ with open(tmp_input_audio_file, "wb") as f:
71
+ f.write(audio_file.getbuffer())
 
 
 
72
 
73
+ # 展示文件到页面上
74
+ # st.audio(tmp_input_audio_file, format="audio/wav")
75
 
76
+ action = st.selectbox("Select Action", ["Add Watermark", "Decode Watermark"])
77
+
78
+ if action == "Add Watermark":
79
+ watermark_text = st.text_input("The watermark (0, 1 list of length-16):", value=st.session_state.def_value)
80
  add_watermark_button = st.button("Add Watermark", key="add_watermark_btn")
81
  if add_watermark_button: # 点击按钮后执行的
82
  if audio_file and watermark_text:
83
  with st.spinner("Adding Watermark..."):
84
+ watermarked_audio, encode_time_cost = add_watermark(tmp_input_audio_file, watermark_text)
 
 
 
 
 
 
 
85
  st.write("Watermarked Audio:")
86
+ print("watermarked_audio:", watermarked_audio)
87
  st.audio(watermarked_audio, format="audio/wav")
88
+ st.write("Time Cost: %d seconds" % encode_time_cost)
89
 
90
  # st.button("Add Watermark", disabled=False)
91
+ elif action == "Decode Watermark":
92
+ if st.button("Decode"):
 
 
 
 
 
 
 
 
 
 
93
  with st.spinner("Decoding..."):
94
+ decode_watermark(tmp_input_audio_file)
 
 
 
 
 
 
 
95
 
96
 
97
  if __name__ == "__main__":
 
99
 
100
  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
101
  model = wavmark.load_model().to(device)
 
102
  main()
103
+
104
+ # audio_path = "/Users/my/Library/Mobile Documents/com~apple~CloudDocs/CODE/PycharmProjects/4_语音水印/419_huggingface水印/WavMark/example.wav"
105
+
106
+ # decoded_watermark, decode_cost = decode_watermark(audio_path)
107
+ # print(decoded_watermark)
example.wav ADDED
Binary file (257 kB). View file
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- wavmark
2
  soundfile
3
  librosa
4
  resampy
 
1
+ wavmark==0.0.2
2
  soundfile
3
  librosa
4
  resampy
停泊/app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pdb
2
+ import time
3
+
4
+ import streamlit as st
5
+ import os
6
+ from utils import wm_add_v2, file_reader, model_util, wm_decode_v2, bin_util
7
+ from models import my_model_v7_recover
8
+ import torch
9
+ import uuid
10
+ import datetime
11
+ import numpy as np
12
+ import soundfile
13
+ from huggingface_hub import hf_hub_download, HfApi
14
+
15
+
16
+ # Function to add watermark to audio
17
+ def add_watermark(audio_path, watermark_text):
18
+ assert len(watermark_text) == 5
19
+
20
+ start_bit, msg_bit, watermark = wm_add_v2.create_parcel_message(len_start_bit, 32, watermark_text)
21
+
22
+ data, sr, audio_length_second = file_reader.read_as_single_channel_16k(audio_path, 16000)
23
+
24
+ _, signal_wmd, time_cost = wm_add_v2.add_watermark(watermark, data, 16000, 0.1, device, model)
25
+
26
+ tmp_file_name = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + "_" + str(uuid.uuid4()) + ".wav"
27
+ tmp_file_path = '/tmp/' + tmp_file_name
28
+ soundfile.write(tmp_file_path, signal_wmd, sr)
29
+ return tmp_file_path
30
+
31
+
32
+ # Function to decode watermark from audio
33
+ def decode_watermark(audio_path):
34
+ data, sr, audio_length_second = file_reader.read_as_single_channel_16k(audio_path, 16000)
35
+ data = data[0:5 * sr]
36
+ start_bit = wm_add_v2.fix_pattern[0:len_start_bit]
37
+ support_count, mean_result, results = wm_decode_v2.extract_watermark_v2(
38
+ data,
39
+ start_bit,
40
+ 0.1,
41
+ 16000,
42
+ 0.3,
43
+ model,
44
+ device, "best")
45
+
46
+ if mean_result is None:
47
+ return "No Watermark"
48
+
49
+ payload = mean_result[len_start_bit:]
50
+ return bin_util.binArray2HexStr(payload)
51
+
52
+
53
+ # Main web app
54
+ def main():
55
+ max_upload_size = 20 * 1024 * 1024 # 20 MB in bytes
56
+
57
+ if "def_value" not in st.session_state:
58
+ st.session_state.def_value = bin_util.binArray2HexStr(np.random.choice([0, 1], size=32 - len_start_bit))
59
+
60
+ st.title("Neural Audio Watermark")
61
+ st.write("Choose the action you want to perform:")
62
+
63
+ action = st.selectbox("Select Action", ["Add Watermark", "Decode Watermark"])
64
+
65
+ if action == "Add Watermark":
66
+ audio_file = st.file_uploader("Upload Audio File (WAV)", type=["wav"], accept_multiple_files=False,
67
+ max_upload_size=max_upload_size)
68
+ if audio_file:
69
+ tmp_input_audio_file = os.path.join("/tmp/", audio_file.name)
70
+ with open(tmp_input_audio_file, "wb") as f:
71
+ f.write(audio_file.getbuffer())
72
+ st.audio(tmp_input_audio_file, format="audio/wav")
73
+
74
+ watermark_text = st.text_input("Enter Watermark Text (5 English letters)", value=st.session_state.def_value)
75
+
76
+ add_watermark_button = st.button("Add Watermark", key="add_watermark_btn")
77
+ if add_watermark_button: # 点击按钮后执行的
78
+ if audio_file and watermark_text:
79
+ with st.spinner("Adding Watermark..."):
80
+ # add_watermark_button.empty()
81
+ # st.button("Add Watermark", disabled=True)
82
+ # st.button("Add Watermark", disabled=True, key="add_watermark_btn_disabled")
83
+ t1 = time.time()
84
+
85
+ watermarked_audio = add_watermark(tmp_input_audio_file, watermark_text)
86
+ encode_time_cost = time.time() - t1
87
+
88
+ st.write("Watermarked Audio:")
89
+ st.audio(watermarked_audio, format="audio/wav")
90
+ st.write("Time Cost:%d seconds" % encode_time_cost)
91
+
92
+ # st.button("Add Watermark", disabled=False)
93
+
94
+ elif action == "Decode Watermark":
95
+ audio_file = st.file_uploader("Upload Audio File (WAV/MP3)", type=["wav", "mp3"], accept_multiple_files=False,
96
+ max_upload_size=max_upload_size)
97
+ if audio_file:
98
+ if st.button("Decode Watermark"):
99
+ # 1.保存
100
+ tmp_file_for_decode_path = os.path.join("/tmp/", audio_file.name)
101
+ with open(tmp_file_for_decode_path, "wb") as f:
102
+ f.write(audio_file.getbuffer())
103
+
104
+ # 2.执行
105
+ with st.spinner("Decoding..."):
106
+ t1 = time.time()
107
+ decoded_watermark = decode_watermark(tmp_file_for_decode_path)
108
+ decode_cost = time.time() - t1
109
+
110
+ print("decoded_watermark", decoded_watermark)
111
+ # Display the decoded watermark
112
+ st.write("Decoded Watermark:", decoded_watermark)
113
+ st.write("Time Cost:%d seconds" % (decode_cost))
114
+
115
+
116
+ def load_model(resume_path):
117
+ n_fft = 1000
118
+ hop_length = 400
119
+ # https://huggingface.co/M4869/InvertibleWM/blob/main/step59000_snr39.99_pesq4.35_BERP_none0.30_mean1.81_std1.81.pkl
120
+ # api_key = st.secrets["api_key"]
121
+ # print(api_key, api_key)
122
+ api_key = "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"
123
+ model_ckpt_path = hf_hub_download(repo_id="M4869/InvertibleWM",
124
+ filename="step59000_snr39.99_pesq4.35_BERP_none0.30_mean1.81_std1.81.pkl",
125
+ token=api_key
126
+ )
127
+ # print("model_ckpt_path", model_ckpt_path)
128
+ resume_path = model_ckpt_path
129
+ # return
130
+
131
+ model = my_model_v7_recover.Model(16000, 32, n_fft, hop_length,
132
+ use_recover_layer=False, num_layers=8).to(device)
133
+ checkpoint = torch.load(resume_path, map_location=torch.device('cpu'))
134
+ state_dict = model_util.map_state_dict(checkpoint['model'])
135
+ model.load_state_dict(state_dict, strict=True)
136
+ model.eval()
137
+ return model
138
+
139
+
140
+ if __name__ == "__main__":
141
+ len_start_bit = 12
142
+
143
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
144
+
145
+ model = load_model("./data/step59000_snr39.99_pesq4.35_BERP_none0.30_mean1.81_std1.81.pkl")
146
+
147
+ main()
148
+ # decode_watermark("/Users/my/Downloads/7a95b353a46893903e9f946c24170b210ce14e8c52c63bb2ab3d144e.wav")
停泊/requirements_bk.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ soundfile
2
+ librosa
3
+ resampy
4
+ torch==1.13.1
5
+ torchvision==0.14.1
6
+ torchaudio==0.13.1