Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from datetime import datetime
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
-
# Mock GoogleGenAI class
|
| 14 |
class GoogleGenAI:
|
| 15 |
def __init__(self, config):
|
| 16 |
self.api_key = config['apiKey']
|
|
@@ -65,7 +65,8 @@ async def generate_music():
|
|
| 65 |
config = data.get('config', {
|
| 66 |
'temperature': 1.1,
|
| 67 |
'topK': 40,
|
| 68 |
-
'guidance': 4.0
|
|
|
|
| 69 |
})
|
| 70 |
|
| 71 |
if not prompts:
|
|
@@ -82,20 +83,29 @@ async def generate_music():
|
|
| 82 |
|
| 83 |
collected_chunks = []
|
| 84 |
total_duration = 0
|
| 85 |
-
target_duration =
|
| 86 |
|
| 87 |
async def mock_generate_chunks():
|
| 88 |
nonlocal collected_chunks, total_duration
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
while total_duration < target_duration:
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
encoded_chunk = base64.b64encode(chunk.tobytes()).decode('utf-8')
|
| 94 |
decoded_chunk = decode(encoded_chunk)
|
| 95 |
collected_chunks.append(decoded_chunk)
|
| 96 |
total_duration += chunk_duration
|
| 97 |
yield {'serverContent': {'audioChunks': [{'data': encoded_chunk}]}}
|
| 98 |
-
await asyncio.sleep(0.1
|
| 99 |
yield {'close': True}
|
| 100 |
|
| 101 |
session = await ai.live.music.connect({
|
|
@@ -128,7 +138,7 @@ async def generate_music():
|
|
| 128 |
|
| 129 |
output = BytesIO(wav_blob)
|
| 130 |
timestamp = datetime.now().isoformat().replace(':', '-')
|
| 131 |
-
filename = f"prompt-dj-music-
|
| 132 |
|
| 133 |
return send_file(
|
| 134 |
output,
|
|
@@ -141,4 +151,4 @@ async def generate_music():
|
|
| 141 |
return jsonify({'error': str(e)}), 500
|
| 142 |
|
| 143 |
if __name__ == '__main__':
|
| 144 |
-
app.run(host='0.0.0.0', port=
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
+
# Mock GoogleGenAI class
|
| 14 |
class GoogleGenAI:
|
| 15 |
def __init__(self, config):
|
| 16 |
self.api_key = config['apiKey']
|
|
|
|
| 65 |
config = data.get('config', {
|
| 66 |
'temperature': 1.1,
|
| 67 |
'topK': 40,
|
| 68 |
+
'guidance': 4.0,
|
| 69 |
+
'slowed_factor': 1.0
|
| 70 |
})
|
| 71 |
|
| 72 |
if not prompts:
|
|
|
|
| 83 |
|
| 84 |
collected_chunks = []
|
| 85 |
total_duration = 0
|
| 86 |
+
target_duration = 60 # 1 minute
|
| 87 |
|
| 88 |
async def mock_generate_chunks():
|
| 89 |
nonlocal collected_chunks, total_duration
|
| 90 |
+
slowed_factor = config.get('slowed_factor', 1.0)
|
| 91 |
+
chunk_duration = 2 * slowed_factor
|
| 92 |
+
samples_per_chunk = int(sample_rate * chunk_duration * 2) # Stereo, 16-bit
|
| 93 |
+
base_freq = 440 # Base frequency (A4)
|
| 94 |
while total_duration < target_duration:
|
| 95 |
+
# Vary frequency based on prompt weights
|
| 96 |
+
total_weight = sum(p['weight'] for p in weighted_prompts)
|
| 97 |
+
freq_modifier = sum(p['weight'] * (ord(p['text'][0]) % 10) / 10 for p in weighted_prompts) / total_weight
|
| 98 |
+
freq = base_freq * (1 + freq_modifier * 0.5) # Adjust frequency slightly per style
|
| 99 |
+
t = np.linspace(0, chunk_duration, samples_per_chunk, False)
|
| 100 |
+
# Simple sine wave with amplitude modulation for "slowed" effect
|
| 101 |
+
amplitude = 0.5 if slowed_factor < 1 else 1.0
|
| 102 |
+
chunk = (amplitude * 32767 * np.sin(2 * np.pi * freq * t / sample_rate)).astype(np.int16)
|
| 103 |
encoded_chunk = base64.b64encode(chunk.tobytes()).decode('utf-8')
|
| 104 |
decoded_chunk = decode(encoded_chunk)
|
| 105 |
collected_chunks.append(decoded_chunk)
|
| 106 |
total_duration += chunk_duration
|
| 107 |
yield {'serverContent': {'audioChunks': [{'data': encoded_chunk}]}}
|
| 108 |
+
await asyncio.sleep(0.1 * slowed_factor)
|
| 109 |
yield {'close': True}
|
| 110 |
|
| 111 |
session = await ai.live.music.connect({
|
|
|
|
| 138 |
|
| 139 |
output = BytesIO(wav_blob)
|
| 140 |
timestamp = datetime.now().isoformat().replace(':', '-')
|
| 141 |
+
filename = f"prompt-dj-music-1min-{timestamp}.wav"
|
| 142 |
|
| 143 |
return send_file(
|
| 144 |
output,
|
|
|
|
| 151 |
return jsonify({'error': str(e)}), 500
|
| 152 |
|
| 153 |
if __name__ == '__main__':
|
| 154 |
+
app.run(host='0.0.0.0', port=7860)
|