Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,20 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
-
from fastapi.responses import
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 5 |
from TTS.api import TTS
|
| 6 |
-
from fastapi.responses import HTMLResponse
|
| 7 |
import uuid
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
# Load chatbot model
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
# Load TTS
|
| 17 |
-
|
| 18 |
-
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 19 |
-
|
| 20 |
-
# ✅ NEW — works immediately in Hugging Face Spaces
|
| 21 |
-
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", gpu=False)
|
| 22 |
|
| 23 |
class ChatRequest(BaseModel):
|
| 24 |
prompt: str
|
|
@@ -28,15 +24,14 @@ class TTSRequest(BaseModel):
|
|
| 28 |
|
| 29 |
@app.post("/chat")
|
| 30 |
async def chat(req: ChatRequest):
|
| 31 |
-
|
| 32 |
-
result = chat_pipeline(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 33 |
return JSONResponse(content={"response": result})
|
| 34 |
|
| 35 |
@app.post("/tts")
|
| 36 |
-
async def
|
| 37 |
-
filename = f"
|
| 38 |
-
tts.tts_to_file(text=req.text,
|
| 39 |
-
return FileResponse(
|
| 40 |
|
| 41 |
@app.get("/", response_class=HTMLResponse)
|
| 42 |
async def root():
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 5 |
from TTS.api import TTS
|
|
|
|
| 6 |
import uuid
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Load chatbot model (TinyLlama is open + small)
|
| 11 |
+
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 14 |
+
pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 15 |
|
| 16 |
+
# Load TTS (Tacotron2 - no license confirmation)
|
| 17 |
+
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
class ChatRequest(BaseModel):
|
| 20 |
prompt: str
|
|
|
|
| 24 |
|
| 25 |
@app.post("/chat")
|
| 26 |
async def chat(req: ChatRequest):
|
| 27 |
+
result = pipeline(req.prompt, max_new_tokens=100)[0]["generated_text"]
|
|
|
|
| 28 |
return JSONResponse(content={"response": result})
|
| 29 |
|
| 30 |
@app.post("/tts")
|
| 31 |
+
async def speak(req: TTSRequest):
|
| 32 |
+
filename = f"speech_{uuid.uuid4().hex}.wav"
|
| 33 |
+
tts.tts_to_file(text=req.text, file_path=filename)
|
| 34 |
+
return FileResponse(filename, media_type="audio/wav", filename=filename)
|
| 35 |
|
| 36 |
@app.get("/", response_class=HTMLResponse)
|
| 37 |
async def root():
|