Update app.py
Browse files
app.py
CHANGED
|
@@ -1,176 +1,73 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
async function start(){
|
| 76 |
-
|
| 77 |
-
const prompt=document.getElementById("prompt").value;
|
| 78 |
-
const duration=document.getElementById("duration").value;
|
| 79 |
-
|
| 80 |
-
const res=await fetch("/generate",{
|
| 81 |
-
method:"POST",
|
| 82 |
-
headers:{"Content-Type":"application/json"},
|
| 83 |
-
body:JSON.stringify({prompt,duration})
|
| 84 |
-
});
|
| 85 |
-
|
| 86 |
-
const data=await res.json();
|
| 87 |
-
jobId=data.job_id;
|
| 88 |
-
|
| 89 |
-
poll();
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
async function poll(){
|
| 93 |
-
|
| 94 |
-
const res=await fetch("/status/"+jobId);
|
| 95 |
-
const data=await res.json();
|
| 96 |
-
|
| 97 |
-
document.getElementById("status").innerText=data.status;
|
| 98 |
-
|
| 99 |
-
if(data.status==="done"){
|
| 100 |
-
|
| 101 |
-
const player=document.getElementById("player");
|
| 102 |
-
player.src=data.download_url;
|
| 103 |
-
player.style.display="block";
|
| 104 |
-
return;
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
if(data.status!=="error"){
|
| 108 |
-
setTimeout(poll,3000);
|
| 109 |
-
}
|
| 110 |
-
}
|
| 111 |
-
</script>
|
| 112 |
-
|
| 113 |
-
</body>
|
| 114 |
-
</html>
|
| 115 |
-
"""
|
| 116 |
-
|
| 117 |
-
# ------------------------------------------------
|
| 118 |
-
# ROUTES
|
| 119 |
-
# ------------------------------------------------
|
| 120 |
-
|
| 121 |
-
@app.route("/")
|
| 122 |
-
def home():
|
| 123 |
-
return render_template_string(HTML)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
@app.route("/generate", methods=["POST"])
|
| 127 |
-
def generate():
|
| 128 |
-
|
| 129 |
-
data = request.json
|
| 130 |
-
|
| 131 |
-
prompt = data.get("prompt")
|
| 132 |
-
duration = int(data.get("duration", 30))
|
| 133 |
-
|
| 134 |
-
job_id = str(uuid.uuid4())
|
| 135 |
-
|
| 136 |
-
jobs[job_id] = {
|
| 137 |
-
"status": "queued",
|
| 138 |
-
"file": None
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
job_queue.put((job_id, prompt, duration))
|
| 142 |
-
|
| 143 |
-
return jsonify({
|
| 144 |
-
"job_id": job_id,
|
| 145 |
-
"status": "queued"
|
| 146 |
-
})
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
@app.route("/status/<job_id>")
|
| 150 |
-
def status(job_id):
|
| 151 |
-
|
| 152 |
-
job = jobs.get(job_id)
|
| 153 |
-
|
| 154 |
-
if not job:
|
| 155 |
-
return jsonify({"error":"job not found"}),404
|
| 156 |
-
|
| 157 |
-
if job["status"] == "done":
|
| 158 |
-
filename = os.path.basename(job["file"])
|
| 159 |
-
|
| 160 |
-
return jsonify({
|
| 161 |
-
"status":"done",
|
| 162 |
-
"download_url":f"/download/{filename}"
|
| 163 |
-
})
|
| 164 |
-
|
| 165 |
-
return jsonify(job)
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
@app.route("/download/<filename>")
|
| 169 |
-
def download(filename):
|
| 170 |
-
return send_file(f"outputs/{filename}", as_attachment=False)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
# ------------------------------------------------
|
| 174 |
-
|
| 175 |
-
if __name__ == "__main__":
|
| 176 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from generate import generate_music
|
| 4 |
+
|
| 5 |
+
# ======================
|
| 6 |
+
# FASTAPI BACKEND
|
| 7 |
+
# ======================
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="AI Background Music Generator")
|
| 10 |
+
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def root():
|
| 13 |
+
return {"status": "running"}
|
| 14 |
+
|
| 15 |
+
@app.post("/generate")
|
| 16 |
+
def api_generate(prompt: str, duration: int = 10):
|
| 17 |
+
file = generate_music(prompt, duration)
|
| 18 |
+
return {"audio": file}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# ======================
|
| 22 |
+
# GRADIO UI
|
| 23 |
+
# ======================
|
| 24 |
+
|
| 25 |
+
def ui_generate(prompt, duration):
|
| 26 |
+
audio = generate_music(prompt, duration)
|
| 27 |
+
return audio
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
with gr.Blocks(
|
| 31 |
+
title="AI Background Music Generator",
|
| 32 |
+
theme=gr.themes.Soft()
|
| 33 |
+
) as demo:
|
| 34 |
+
|
| 35 |
+
gr.Markdown("""
|
| 36 |
+
# 🎵 AI Background Music Generator
|
| 37 |
+
Generate royalty-free background music for:
|
| 38 |
+
- Islamic motivation videos
|
| 39 |
+
- Reels
|
| 40 |
+
- TikTok
|
| 41 |
+
- YouTube Shorts
|
| 42 |
+
""")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
prompt = gr.Textbox(
|
| 46 |
+
label="Music Description",
|
| 47 |
+
placeholder="Soft emotional nasheed style, cinematic ambient..."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
duration = gr.Slider(
|
| 51 |
+
5,
|
| 52 |
+
30,
|
| 53 |
+
value=10,
|
| 54 |
+
step=1,
|
| 55 |
+
label="Duration (seconds)"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
generate_btn = gr.Button("Generate Music")
|
| 59 |
+
|
| 60 |
+
output_audio = gr.Audio(
|
| 61 |
+
label="Generated Audio",
|
| 62 |
+
type="filepath"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
generate_btn.click(
|
| 66 |
+
fn=ui_generate,
|
| 67 |
+
inputs=[prompt, duration],
|
| 68 |
+
outputs=output_audio
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Mount Gradio inside FastAPI
|
| 73 |
+
app = gr.mount_gradio_app(app, demo, path="/ui")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|