Zw07 commited on
Commit
f807785
·
verified ·
1 Parent(s): 80f55a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -202
app.py CHANGED
@@ -14,17 +14,6 @@ import io
14
  import librosa
15
  import ffmpeg
16
 
17
- #from torchaudio.io import CodecConfig
18
- # import numpy
19
-
20
- # def my_read_file(audio_path, max_second):
21
- # signal, sr, audio_length_second = read_as_single_channel_16k(audio_path, default_sr)
22
- # if audio_length_second > max_second:
23
- # signal = signal[0:default_sr * max_second]
24
- # audio_length_second = max_second
25
-
26
- # return signal, sr, audio_length_second
27
-
28
  def create_default_value():
29
  if "def_value" not in st.session_state:
30
  def_val_npy = np.random.choice([0, 1], size=32 - len_start_bit)
@@ -98,18 +87,6 @@ def main():
98
  file_extension_ori =".mp3"
99
  file_extension =".wav"
100
 
101
- #RuntimeError: Could not infer dtype of numpy.float32
102
- #wav = torch.tensor(wav3).float() / 32768.0
103
-
104
- #RuntimeError: Numpy is not available
105
- # wav = torch.from_numpy(wav3) #/32768.0
106
- # wav = wav.unsqueeze(0).unsqueeze(0)
107
- # st.markdown("Before unsqueeze mp3")
108
- # st.markdown(wav)
109
-
110
- #Unsqueeze for line 176
111
- # wav= wav.unsqueeze(0)
112
-
113
  action = st.selectbox("Select Action", ["Add Watermark", "Detect Watermark"])
114
 
115
  if action == "Add Watermark":
@@ -205,179 +182,6 @@ def main():
205
 
206
  except RuntimeError:
207
  st.error("Please input audio with one channel (mono-channel)")
208
-
209
- # if audio_file:
210
- # # 保存文件到本地:
211
- # # tmp_input_audio_file = os.path.join("/tmp/", audio_file.name)
212
- # # st.markdown(tmp_input_audio_file)
213
- # # with open(tmp_input_audio_file, "wb") as f:
214
- # # f.write(audio_file.getbuffer())
215
- # # st.audio(tmp_input_audio_file, format="mp3/wav")
216
-
217
-
218
- # #1st attempt
219
- # #audio_path = " audio_file.name"
220
-
221
- # # audio, sr = torchaudio.load(audio_file)
222
- # # st.audio(audio_file, format="audio/mpeg")
223
- # # audio= audio.unsqueeze(0)
224
-
225
- # # st.markdown("SR")
226
- # # st.markdown(sr)
227
- # # st.markdown("after unsqueeze wav or mp3")
228
- # # st.markdown(audio)
229
-
230
- # #2nd attempt
231
- # # Save file to local storage
232
- # tmp_input_audio_file = os.path.join("/tmp/", audio_file.name)
233
- # file_extension = os.path.splitext(tmp_input_audio_file)[1].lower()
234
- # #st.markdown(file_extension)
235
- # if file_extension in [".wav", ".flac"]:
236
- # with open("test.wav", "wb") as f:
237
- # f.write(audio_file.getbuffer())
238
-
239
- # st.audio("test.wav", format="audio/wav")
240
-
241
- # elif file_extension == ".mp3":
242
- # with open("test.mp3", "wb") as f:
243
- # f.write(audio_file.getbuffer())
244
-
245
- # st.audio("test.mp3", format="audio/mpeg")
246
-
247
- # #Load the WAV file using torchaudio
248
- # if file_extension in [".wav", ".flac"]:
249
- # wav, sample_rate = torchaudio.load("test.wav")
250
- # # st.markdown("Before unsquueze wav")
251
- # # st.markdown(wav)
252
- # file_extension_ori =".wav"
253
- # #Unsqueeze for line 176
254
- # wav= wav.unsqueeze(0)
255
-
256
- # elif file_extension == ".mp3":
257
- # # Load an MP3 file
258
- # audio = AudioSegment.from_mp3("test.mp3")
259
-
260
- # # Export it as a WAV file
261
- # audio.export("test.wav", format="wav")
262
- # wav3, sample_rate = torchaudio.load("test.wav")
263
- # wav= wav3.unsqueeze(0)
264
- # file_extension_ori =".mp3"
265
- # file_extension =".wav"
266
-
267
- # #RuntimeError: Could not infer dtype of numpy.float32
268
- # #wav = torch.tensor(wav3).float() / 32768.0
269
-
270
- # #RuntimeError: Numpy is not available
271
- # # wav = torch.from_numpy(wav3) #/32768.0
272
- # # wav = wav.unsqueeze(0).unsqueeze(0)
273
- # # st.markdown("Before unsqueeze mp3")
274
- # # st.markdown(wav)
275
-
276
- # #Unsqueeze for line 176
277
- # # wav= wav.unsqueeze(0)
278
-
279
- # action = st.selectbox("Select Action", ["Add Watermark", "Detect Watermark"])
280
-
281
- # if action == "Add Watermark":
282
- # #watermark_text = st.text_input("The watermark (0, 1 list of length-16):", value=st.session_state.def_value)
283
- # add_watermark_button = st.button("Add Watermark", key="add_watermark_btn")
284
- # if add_watermark_button: # 点击按钮后执行的
285
- # #if audio_file and watermark_text:
286
- # if audio_file:
287
- # with st.spinner("Adding Watermark..."):
288
- # #wav = my_read_file(wav,max_second_encode)
289
-
290
- # #1st attempt
291
- # watermark = model.get_watermark(wav, default_sr)
292
- # watermarked_audio = wav + watermark
293
- # print(watermarked_audio.size())
294
- # size = watermarked_audio.size()
295
- # #st.markdown(size)
296
-
297
- # print(watermarked_audio.squeeze())
298
- # squeeze = watermarked_audio.squeeze(1)
299
- # shape = squeeze.size()
300
- # #st.markdown(shape)
301
-
302
- # #st.markdown(squeeze)
303
- # if file_extension_ori in [".wav", ".flac"]:
304
- # torchaudio.save("output.wav", squeeze, default_sr, bits_per_sample=16)
305
- # watermarked_wav = torchaudio.save("output.wav", squeeze, default_sr, bits_per_sample=16)
306
-
307
- # st.audio("output.wav", format="audio/wav")
308
-
309
- # with open("output.wav", "rb") as file:
310
- # #file.read()
311
- # #file.write(watermarked_wav.getbuffer())
312
- # binary_data = file.read()
313
- # btn = st.download_button(
314
- # label="Download watermarked audio",
315
- # data=binary_data,
316
- # file_name="output.wav",
317
- # mime="audio/wav",
318
- # )
319
-
320
-
321
- # elif file_extension_ori == ".mp3":
322
- # torchaudio.save("output.wav", squeeze, default_sr)
323
- # watermarked_mp3 = torchaudio.save("output.wav", squeeze, default_sr)
324
- # audio = AudioSegment.from_wav("output.wav")
325
-
326
- # # Export as MP3
327
- # audio.export("output.mp3", format="mp3")
328
- # st.audio("output.mp3", format="audio/mpeg")
329
-
330
- # with open("output.mp3", "rb") as file:
331
- # #file.write(watermarked_wav.getbuffer())
332
- # binary_data = file.read()
333
- # st.download_button(
334
- # label="Download watermarked audio",
335
- # data=binary_data,
336
- # file_name="output.mp3",
337
- # mime="audio/mpeg",
338
- # )
339
- # # except RuntimeError:
340
- # # st.error("Please input audio with one channel (mono-channel)")
341
-
342
- # elif action == "Detect Watermark":
343
- # detect_watermark_button = st.button("Detect Watermark", key="detect_watermark_btn")
344
-
345
- # # if audio_file:
346
- # # #1st attempt
347
- # # watermark = model.get_watermark(wav, default_sr)
348
- # # watermarked_audio = wav + watermark
349
- # # print(watermarked_audio.size())
350
- # # size = watermarked_audio.size()
351
- # # #st.markdown(size)
352
-
353
-
354
- # if detect_watermark_button:
355
- # with st.spinner("Detecting..."):
356
- # # result, message = detector.detect_watermark(watermarked_audio, sample_rate=default_sr, message_threshold=0.5)
357
- # # st.markdown("Probability of audio being watermarked: ")
358
- # # st.markdown(result)
359
- # # st.markdown("This is likely a watermarked audio!")
360
- # # print(f"\nThis is likely a watermarked audio: {result}")
361
-
362
- # #Run on an unwatermarked audio
363
-
364
- # if file_extension in [".wav", ".flac"]:
365
- # wav, sample_rate = torchaudio.load("test.wav")
366
- # wav= wav.unsqueeze(0)
367
-
368
- # elif file_extension == ".mp3":
369
- # # Load an MP3 file
370
- # audio = AudioSegment.from_mp3("test.mp3")
371
- # # Export it as a WAV file
372
- # audio.export("test.wav", format="wav")
373
- # wav, sample_rate = torchaudio.load("test.wav")
374
- # wav= wav.unsqueeze(0)
375
-
376
- # result2, message2 = detector.detect_watermark(wav, sample_rate=default_sr, message_threshold=0.5)
377
- # print(f"This is likely an unwatermarked audio: {result2}")
378
- # st.markdown("Probability of audio being watermarked: ")
379
- # st.markdown(result2)
380
- # st.markdown("This is likely an unwatermarked audio!")
381
 
382
 
383
  if __name__ == "__main__":
@@ -389,9 +193,4 @@ if __name__ == "__main__":
389
  # model = wavmark.load_model().to(device)
390
  model = AudioSeal.load_generator("audioseal_wm_16bits")
391
  detector = AudioSeal.load_detector(("audioseal_detector_16bits"))
392
- main()
393
-
394
- # audio_path = "/Users/my/Library/Mobile Documents/com~apple~CloudDocs/CODE/PycharmProjects/4_语音水印/419_huggingface水印/WavMark/example.wav"
395
-
396
- # decoded_watermark, decode_cost = decode_watermark(audio_path)
397
- # print(decoded_watermark)
 
14
  import librosa
15
  import ffmpeg
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  def create_default_value():
18
  if "def_value" not in st.session_state:
19
  def_val_npy = np.random.choice([0, 1], size=32 - len_start_bit)
 
87
  file_extension_ori =".mp3"
88
  file_extension =".wav"
89
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  action = st.selectbox("Select Action", ["Add Watermark", "Detect Watermark"])
91
 
92
  if action == "Add Watermark":
 
182
 
183
  except RuntimeError:
184
  st.error("Please input audio with one channel (mono-channel)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
 
187
  if __name__ == "__main__":
 
193
  # model = wavmark.load_model().to(device)
194
  model = AudioSeal.load_generator("audioseal_wm_16bits")
195
  detector = AudioSeal.load_detector(("audioseal_detector_16bits"))
196
+ main()