Noobjay commited on
Commit
e67afbf
·
verified ·
1 Parent(s): 6562282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -1,17 +1,36 @@
1
- import gradio as gr
2
- from bark import generate_audio, preload_models
 
 
3
 
4
- # Load models once on startup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  preload_models()
6
 
7
- def bark_tts(text):
8
- audio_array = generate_audio(text)
9
- return (44100, audio_array) # returns tuple (sample_rate, numpy array)
10
 
11
- gr.Interface(
12
- fn=bark_tts,
13
- inputs=gr.Textbox(lines=2, placeholder="Enter text here...", label="Text Input"),
14
- outputs=gr.Audio(label="Bark TTS Output"),
15
- title="🐶 Bark TTS Demo",
16
- description="Generate realistic speech using [Suno's Bark](https://github.com/suno-ai/bark) model.",
17
- ).launch()
 
1
+ from bark import SAMPLE_RATE, generate_audio, preload_models
2
+ from scipy.io.wavfile import write as write_wav
3
+ import torch
4
+ import os
5
 
6
+ from fastapi import FastAPI, Request, Form
7
+ from fastapi.responses import FileResponse
8
+ from fastapi.staticfiles import StaticFiles
9
+ from fastapi.templating import Jinja2Templates
10
+
11
+ # Patch torch.load for PyTorch 2.6+
12
+ if hasattr(torch, 'serialization'):
13
+ torch.serialization.add_safe_globals([
14
+ "numpy.core.multiarray.scalar"
15
+ ])
16
+
17
+ # Create FastAPI app
18
+ app = FastAPI()
19
+
20
+ # Mount static and templates folder
21
+ app.mount("/static", StaticFiles(directory="static"), name="static")
22
+ templates = Jinja2Templates(directory="templates")
23
+
24
+ # Preload Bark models
25
  preload_models()
26
 
27
+ @app.get("/")
28
+ def index(request: Request):
29
+ return templates.TemplateResponse("index.html", {"request": request})
30
 
31
+ @app.post("/synthesize")
32
+ def synthesize(request: Request, text: str = Form(...)):
33
+ audio_array = generate_audio(text)
34
+ output_path = "static/output.wav"
35
+ write_wav(output_path, SAMPLE_RATE, audio_array)
36
+ return FileResponse(output_path, media_type="audio/wav", filename="output.wav")