basyx commited on
Commit
b2bbd1e
·
verified ·
1 Parent(s): fef7c67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -176
app.py CHANGED
@@ -1,176 +1,73 @@
1
- from flask import Flask, request, jsonify, send_file, render_template_string
2
- import uuid, os
3
-
4
- from worker import job_queue, jobs
5
-
6
- app = Flask(__name__)
7
-
8
- # ------------------------------------------------
9
- # PRODUCTION UI
10
- # ------------------------------------------------
11
-
12
- HTML = """
13
- <!DOCTYPE html>
14
- <html>
15
- <head>
16
- <title>MusicGen Studio</title>
17
-
18
- <style>
19
- body{
20
- background:#020617;
21
- color:white;
22
- font-family:sans-serif;
23
- text-align:center;
24
- padding:40px;
25
- }
26
-
27
- .container{max-width:700px;margin:auto;}
28
-
29
- textarea,input,select{
30
- width:100%;
31
- padding:12px;
32
- margin-top:10px;
33
- border-radius:8px;
34
- border:none;
35
- }
36
-
37
- button{
38
- background:#22c55e;
39
- padding:14px;
40
- width:100%;
41
- border:none;
42
- margin-top:15px;
43
- border-radius:8px;
44
- color:white;
45
- font-size:16px;
46
- cursor:pointer;
47
- }
48
-
49
- audio{width:100%;margin-top:20px;}
50
- </style>
51
- </head>
52
-
53
- <body>
54
-
55
- <div class="container">
56
- <h2>🎵 MusicGen Production Studio</h2>
57
-
58
- <textarea id="prompt" rows="4"
59
- placeholder="soft emotional nasheed background, peaceful cinematic"></textarea>
60
-
61
- <input id="duration" type="number" value="30" min="5" max="60">
62
-
63
- <button onclick="start()">Generate</button>
64
-
65
- <p id="status"></p>
66
-
67
- <audio id="player" controls style="display:none"></audio>
68
-
69
- </div>
70
-
71
- <script>
72
-
73
- let jobId=null;
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")