Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -883,5 +883,58 @@ gr.Interface(
|
|
| 883 |
allow_flagging="never"
|
| 884 |
).launch()
|
| 885 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 886 |
# Launch Gradio App
|
| 887 |
demo.launch()
|
|
|
|
| 883 |
allow_flagging="never"
|
| 884 |
).launch()
|
| 885 |
|
| 886 |
+
# === Hugging Face API Integration ===
|
| 887 |
+
def hf_api_process(audio_data_url, effects_json, isolate, preset, export_format):
|
| 888 |
+
import base64, tempfile, json, os
|
| 889 |
+
from pydub import AudioSegment
|
| 890 |
+
|
| 891 |
+
try:
|
| 892 |
+
# Step 1: Decode base64 audio string
|
| 893 |
+
header, base64_data = audio_data_url.split(",", 1)
|
| 894 |
+
audio_bytes = base64.b64decode(base64_data)
|
| 895 |
+
suffix = ".mp3" if "mpeg" in header else ".wav"
|
| 896 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
|
| 897 |
+
f.write(audio_bytes)
|
| 898 |
+
input_path = f.name
|
| 899 |
+
|
| 900 |
+
# Step 2: Parse effects
|
| 901 |
+
if isinstance(effects_json, str):
|
| 902 |
+
effects = json.loads(effects_json)
|
| 903 |
+
else:
|
| 904 |
+
effects = effects_json
|
| 905 |
+
|
| 906 |
+
# Step 3: Call the processing function
|
| 907 |
+
output_path, _, _, _, _, _ = process_audio(
|
| 908 |
+
input_path, effects, isolate, preset, export_format
|
| 909 |
+
)
|
| 910 |
+
|
| 911 |
+
# Step 4: Convert result to base64
|
| 912 |
+
with open(output_path, "rb") as f:
|
| 913 |
+
out_b64 = base64.b64encode(f.read()).decode()
|
| 914 |
+
mime = "audio/wav" if export_format.lower() == "wav" else "audio/mpeg"
|
| 915 |
+
return f"data:{mime};base64,{out_b64}"
|
| 916 |
+
|
| 917 |
+
except Exception as e:
|
| 918 |
+
return f"Error: {str(e)}"
|
| 919 |
+
|
| 920 |
+
# Define an API interface for Hugging Face Spaces
|
| 921 |
+
api_interface = gr.Interface(
|
| 922 |
+
fn=hf_api_process,
|
| 923 |
+
inputs=[
|
| 924 |
+
gr.Text(label="Audio Base64 Data URL"),
|
| 925 |
+
gr.Textbox(label="Effects (JSON)"),
|
| 926 |
+
gr.Checkbox(label="Isolate Vocals"),
|
| 927 |
+
gr.Textbox(label="Preset"),
|
| 928 |
+
gr.Textbox(label="Export Format")
|
| 929 |
+
],
|
| 930 |
+
outputs=gr.Text(label="Processed Audio as Base64 URL"),
|
| 931 |
+
allow_flagging="never"
|
| 932 |
+
)
|
| 933 |
+
|
| 934 |
+
# Include this in the Blocks launch to enable API access
|
| 935 |
+
demo.launch(show_api=True)
|
| 936 |
+
api_interface.launch(inline=False, share=False)
|
| 937 |
+
|
| 938 |
+
|
| 939 |
# Launch Gradio App
|
| 940 |
demo.launch()
|