CleanSong commited on
Commit
2b9309b
·
verified ·
1 Parent(s): 3437939

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -43
app.py CHANGED
@@ -1,60 +1,35 @@
1
- import gradio as gr
2
- import requests
3
- import os
4
- import time
5
  from gradio_client import Client
 
6
 
7
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
8
 
9
- # Backend endpoint
10
- BACKEND_SPACE_API = (
11
- "https://hf.space/embed/CleanSong-AI/Main-tool-backend-main/api/predict/"
12
- )
13
-
14
- # ------------------------------------------------------------
15
- # (Optional) Support function if you actually need this later
16
- # ------------------------------------------------------------
17
- def call_space(space_repo, input_data, param_name="file_path"):
18
- client = Client(space_repo, hf_token=HF_TOKEN)
19
- start = time.time()
20
-
21
- if isinstance(input_data, dict):
22
- kwargs = {k: v for k, v in input_data.items()}
23
- else:
24
- kwargs = {param_name: input_data}
25
-
26
- try:
27
- result = client.predict(api_name="/predict", **kwargs)
28
- except Exception as e:
29
- raise Exception(f"Space call failed: {e}")
30
-
31
- return result
32
-
33
- # ------------------------------------------------------------
34
- # Main function called by Gradio
35
- # ------------------------------------------------------------
36
  def clean_song(file_path):
37
- """
38
- Sends uploaded audio file to the HF backend and returns cleaned version.
39
- """
40
  if file_path is None:
41
  return None
42
 
43
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
 
 
44
 
45
  try:
46
- with open(file_path, "rb") as f:
47
- files = {"data": (os.path.basename(file_path), f, "audio/mpeg")}
48
- response = requests.post(BACKEND_SPACE_API, files=files, headers=headers)
49
- response.raise_for_status()
 
50
  except Exception as e:
51
- return f"Error: {e}"
52
 
53
- cleaned_bytes = response.content
 
 
54
 
55
- output_path = "cleaned_output.mp3"
 
56
  with open(output_path, "wb") as f:
57
- f.write(cleaned_bytes)
58
 
59
  return output_path
60
 
 
 
 
 
 
1
  from gradio_client import Client
2
+ import os
3
 
4
  HF_TOKEN = os.environ.get("HF_TOKEN")
5
+ BACKEND_SPACE = "CleanSong-AI/Main-tool-backend-main"
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def clean_song(file_path):
 
 
 
8
  if file_path is None:
9
  return None
10
 
11
+ client = Client(
12
+ BACKEND_SPACE,
13
+ hf_token=HF_TOKEN
14
+ )
15
 
16
  try:
17
+ # This EXACTLY matches how Gradio uploads files
18
+ result = client.predict(
19
+ file_path,
20
+ api_name="/predict"
21
+ )
22
  except Exception as e:
23
+ return f"Error calling backend: {e}"
24
 
25
+ # result is usually a filepath OR bytes depending on backend
26
+ if isinstance(result, str) and os.path.exists(result):
27
+ return result
28
 
29
+ # fallback: save bytes
30
+ output_path = "cleaned_output.wav"
31
  with open(output_path, "wb") as f:
32
+ f.write(result)
33
 
34
  return output_path
35