shahid202 commited on
Commit
aefbcc8
Β·
verified Β·
1 Parent(s): e3e2a7a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -14
main.py CHANGED
@@ -6,22 +6,21 @@ from fastapi import FastAPI
6
  from fastapi.staticfiles import StaticFiles
7
  from pydantic import BaseModel
8
  from transformers import pipeline
9
- from kokoro import KPipeline
10
 
11
  app = FastAPI()
12
 
13
- # Setup static folder
14
  if not os.path.exists("static"):
15
  os.makedirs("static", exist_ok=True)
16
  app.mount("/static", StaticFiles(directory="static"), name="static")
17
 
18
- # Load Models
19
  print("Loading SmolLM2...")
20
  chat_pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct", device="cpu")
21
 
22
- print("Loading Kokoro...")
23
- # 'a' stands for American English
24
- tts_pipe = KPipeline(lang_code='a')
25
 
26
  class ChatRequest(BaseModel):
27
  message: str
@@ -29,21 +28,19 @@ class ChatRequest(BaseModel):
29
 
30
  @app.post("/chat")
31
  async def chat_and_tts(request: ChatRequest):
32
- # 1. Text Generation
33
  prompt = f"You are Bella, a smart and witty female AI assistant. Keep it very short.\nUser: {request.message}\nBella:"
34
  outputs = chat_pipe(prompt, max_new_tokens=100, temperature=0.3, do_sample=True)
35
  full_text = outputs[0]['generated_text'].split("Bella:")[-1].split("User:")[0].strip()
36
 
37
- # 2. Audio Generation (Starts only after full_text is complete)
38
- generator = tts_pipe(full_text, voice=request.voice, speed=1.0)
39
- audio_chunks = [audio for _, _, audio in generator]
40
- combined_audio = np.concatenate(audio_chunks)
41
 
42
  file_path = "static/output.wav"
43
- sf.write(file_path, combined_audio, 24000)
44
 
45
- # 3. Final Response
46
- # Change 'shahid202' to your actual space path
47
  space_url = "https://shahid202-all.hf.space"
48
 
49
  return {
 
6
  from fastapi.staticfiles import StaticFiles
7
  from pydantic import BaseModel
8
  from transformers import pipeline
9
+ from kokoro_onnx import Kokoro
10
 
11
  app = FastAPI()
12
 
 
13
  if not os.path.exists("static"):
14
  os.makedirs("static", exist_ok=True)
15
  app.mount("/static", StaticFiles(directory="static"), name="static")
16
 
17
+ # Load SmolLM2 for Text
18
  print("Loading SmolLM2...")
19
  chat_pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct", device="cpu")
20
 
21
+ # Load Kokoro ONNX (Bypasses misaki/espeak errors)
22
+ print("Loading Kokoro ONNX...")
23
+ kokoro = Kokoro("kokoro-v1.0.onnx", "voices.bin")
24
 
25
  class ChatRequest(BaseModel):
26
  message: str
 
28
 
29
  @app.post("/chat")
30
  async def chat_and_tts(request: ChatRequest):
31
+ # 1. Generate Text
32
  prompt = f"You are Bella, a smart and witty female AI assistant. Keep it very short.\nUser: {request.message}\nBella:"
33
  outputs = chat_pipe(prompt, max_new_tokens=100, temperature=0.3, do_sample=True)
34
  full_text = outputs[0]['generated_text'].split("Bella:")[-1].split("User:")[0].strip()
35
 
36
+ # 2. Generate Audio via ONNX
37
+ samples, sample_rate = kokoro.create(full_text, voice=request.voice, speed=1.0, lang="en-us")
 
 
38
 
39
  file_path = "static/output.wav"
40
+ sf.write(file_path, samples, sample_rate)
41
 
42
+ # 3. Response
43
+ # Change 'shahid202' to your actual space name if different
44
  space_url = "https://shahid202-all.hf.space"
45
 
46
  return {