Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import io
|
| 4 |
+
import tempfile
|
| 5 |
+
from elevenlabs import ElevenLabs
|
| 6 |
+
import numpy as np
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
|
| 9 |
+
def get_elevenlabs_client(api_key):
|
| 10 |
+
"""Skapa ElevenLabs-klient med API-nyckel"""
|
| 11 |
+
return ElevenLabs(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
def generate_sound_effect(text_prompt, api_key, duration=30.0):
|
| 14 |
+
"""Generera ljudeffekt med ElevenLabs Sound Effects API"""
|
| 15 |
+
try:
|
| 16 |
+
client = get_elevenlabs_client(api_key)
|
| 17 |
+
|
| 18 |
+
# Generera ljud från text
|
| 19 |
+
audio_data = client.text_to_sound_effects.convert(
|
| 20 |
+
text=text_prompt,
|
| 21 |
+
duration_seconds=duration
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Spara till temporär fil
|
| 25 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
|
| 26 |
+
tmp_file.write(audio_data.read())
|
| 27 |
+
return tmp_file.name
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
raise Exception(f"Fel vid generering: {str(e)}")
|
| 31 |
+
|
| 32 |
+
# Skapa Gradio-gränssnitt
|
| 33 |
+
def create_interface():
|
| 34 |
+
with gr.Blocks(title="ElevenLabs Sound Effects Editor") as demo:
|
| 35 |
+
gr.Markdown("# 🎵 ElevenLabs Sound Effects Editor")
|
| 36 |
+
gr.Markdown("Använd ElevenLabs AI för att generera ljudeffekter!")
|
| 37 |
+
|
| 38 |
+
# API-nyckel input
|
| 39 |
+
api_key_input = gr.Textbox(
|
| 40 |
+
label="ElevenLabs API-nyckel",
|
| 41 |
+
type="password",
|
| 42 |
+
placeholder="Skriv in din ElevenLabs API-nyckel här...",
|
| 43 |
+
info="Du hittar din API-nyckel på https://elevenlabs.io/api"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Huvudgränssnitt
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
prompt_input = gr.Textbox(
|
| 50 |
+
label="Text-prompt",
|
| 51 |
+
placeholder="Ex: Cinematic explosion, Rain falling, Sci-fi laser",
|
| 52 |
+
lines=3
|
| 53 |
+
)
|
| 54 |
+
duration_slider = gr.Slider(
|
| 55 |
+
minimum=1.0,
|
| 56 |
+
maximum=10.0,
|
| 57 |
+
value=3.0,
|
| 58 |
+
step=0.5,
|
| 59 |
+
label="Varaktighet (sekunder)"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
with gr.Column():
|
| 63 |
+
audio_output = gr.Audio(label="Genererad ljudeffekt")
|
| 64 |
+
|
| 65 |
+
generate_btn = gr.Button("🔊 Generera ljud", variant="primary")
|
| 66 |
+
generate_btn.click(
|
| 67 |
+
fn=generate_sound_effect,
|
| 68 |
+
inputs=[prompt_input, api_key_input, duration_slider],
|
| 69 |
+
outputs=audio_output
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Tips
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
### 💡 Tips för bra prompts:
|
| 75 |
+
- **Miljö:** "Rain falling on leaves", "Ocean waves", "Forest birds"
|
| 76 |
+
- **Effekter:** "Explosion with debris", "Laser gun firing", "Magic sparkle"
|
| 77 |
+
- **Föremål:** "Door creaking", "Keys jangling", "Pages turning"
|
| 78 |
+
""")
|
| 79 |
+
|
| 80 |
+
return demo
|
| 81 |
+
|
| 82 |
+
# Skapa och kör appen
|
| 83 |
+
demo = create_interface()
|
| 84 |
+
demo.launch()
|