Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -875,8 +875,8 @@ def hf_api_process(audio_data_url, effects_json, isolate, preset, export_format)
|
|
| 875 |
except Exception as e:
|
| 876 |
return f"Error: {str(e)}"
|
| 877 |
|
| 878 |
-
#
|
| 879 |
-
|
| 880 |
fn=hf_api_process,
|
| 881 |
inputs=[
|
| 882 |
gr.Text(label="Audio Base64 Data URL"),
|
|
@@ -887,13 +887,39 @@ api_interface = gr.Interface(
|
|
| 887 |
],
|
| 888 |
outputs=gr.Text(label="Processed Audio as Base64 URL"),
|
| 889 |
allow_flagging="never"
|
| 890 |
-
)
|
| 891 |
|
| 892 |
-
# Launch both demo and API interface using gr.Parallel
|
| 893 |
-
demo.queue()
|
| 894 |
-
api_interface.queue()
|
| 895 |
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
except Exception as e:
|
| 876 |
return f"Error: {str(e)}"
|
| 877 |
|
| 878 |
+
# Add standalone API interface for Hugging Face to access
|
| 879 |
+
gr.Interface(
|
| 880 |
fn=hf_api_process,
|
| 881 |
inputs=[
|
| 882 |
gr.Text(label="Audio Base64 Data URL"),
|
|
|
|
| 887 |
],
|
| 888 |
outputs=gr.Text(label="Processed Audio as Base64 URL"),
|
| 889 |
allow_flagging="never"
|
| 890 |
+
).launch(inline=False, share=False)
|
| 891 |
|
|
|
|
|
|
|
|
|
|
| 892 |
|
| 893 |
+
# === Hugging Face API Integration ===
|
| 894 |
+
def hf_api_process(audio_data_url, effects_json, isolate, preset, export_format):
|
| 895 |
+
try:
|
| 896 |
+
import base64, tempfile, json
|
| 897 |
+
from pydub import AudioSegment
|
| 898 |
+
header, base64_data = audio_data_url.split(",", 1)
|
| 899 |
+
audio_bytes = base64.b64decode(base64_data)
|
| 900 |
+
suffix = ".mp3" if "mpeg" in header else ".wav"
|
| 901 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
|
| 902 |
+
f.write(audio_bytes)
|
| 903 |
+
input_path = f.name
|
| 904 |
+
effects = json.loads(effects_json) if isinstance(effects_json, str) else effects_json
|
| 905 |
+
output_path, *_ = process_audio(input_path, effects, isolate, preset, export_format)
|
| 906 |
+
with open(output_path, "rb") as f:
|
| 907 |
+
out_b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 908 |
+
mime = "audio/wav" if export_format.lower() == "wav" else "audio/mpeg"
|
| 909 |
+
return f"data:{mime};base64,{out_b64}"
|
| 910 |
+
except Exception as e:
|
| 911 |
+
return f"Error: {str(e)}"
|
| 912 |
+
|
| 913 |
+
# Add standalone API interface for Hugging Face to access
|
| 914 |
+
gr.Interface(
|
| 915 |
+
fn=hf_api_process,
|
| 916 |
+
inputs=[
|
| 917 |
+
gr.Text(label="Audio Base64 Data URL"),
|
| 918 |
+
gr.Textbox(label="Effects (JSON)"),
|
| 919 |
+
gr.Checkbox(label="Isolate Vocals"),
|
| 920 |
+
gr.Textbox(label="Preset"),
|
| 921 |
+
gr.Textbox(label="Export Format")
|
| 922 |
+
],
|
| 923 |
+
outputs=gr.Text(label="Processed Audio as Base64 URL"),
|
| 924 |
+
allow_flagging="never"
|
| 925 |
+
).launch(inline=False, share=False)
|