Skip to content

Commit

Permalink
Add Redux and Podcast API
Browse files Browse the repository at this point in the history
  • Loading branch information
LERM0 committed Jun 3, 2024
1 parent cef1a6d commit a37b491
Show file tree
Hide file tree
Showing 22 changed files with 1,597 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ yarn-error.log*
*.pem
.DS_Store

__pycache__
__pycache__
tmp
6 changes: 6 additions & 0 deletions LOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Run with docker
```sh
docker build -t lerm0/core . --platform=linux/amd64
docker run -it -v /Users/LermoAI/apps/api/core-api:/app lerm0/core bash
```
4 changes: 3 additions & 1 deletion apps/api/core-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ COPY . .
RUN mkdir /.local && chmod 777 /.local
RUN mkdir /.cache && chmod 777 /.cache
RUN pip install -r requirements.txt
RUN pip install git+https://github.com/myshell-ai/MeloTTS.git
RUN python -m unidic download

EXPOSE 8000

ENTRYPOINT python main.py
# ENTRYPOINT python main.py
24 changes: 24 additions & 0 deletions apps/api/core-api/app/api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from fastapi import APIRouter, HTTPException, Depends, Header, Request
from fastapi.responses import StreamingResponse, FileResponse

from app.engine import create_podcast_script, text_to_voice

router = APIRouter()

@router.post("/podcast")
async def chat_with_model(request: Request):
try:
params = await request.json()
prompt = params.get("prompt")
content = params.get("content")
text = create_podcast_script(content)

prompt_array = prompt.split(", ")[:2]
file_name = prompt_array[0]
speaker = prompt_array[1]
file_path_full = f"/tmp/voice/{file_name}.wav"

text_to_voice(text, file_path_full, speaker)
return FileResponse(path=file_path_full, media_type="audio/wav", filename=f"{file_name}.wav")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
27 changes: 27 additions & 0 deletions apps/api/core-api/app/engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from langchain_openai import ChatOpenAI
from melo.api import TTS


llm = None
VOICE_ENGINE = None
device = 'cpu'
def init_engine():
global llm
global VOICE_ENGINE
if not llm:
llm = ChatOpenAI(model="gpt4", temperature=0)
if not VOICE_ENGINE:
VOICE_ENGINE = TTS(language='EN', device=device)

def create_podcast_script(content: str):
prompt = """
Go deep into {content} and explain like human talk max 300 words.
"""
result = llm.invoke(prompt.format(content=content))
return result.content

def text_to_voice(text: str, file_path: str, speaker: str):
speed = 1
speaker_ids = VOICE_ENGINE.hps.data.spk2id
VOICE_ENGINE.tts_to_file(text, speaker_ids[speaker], file_path, speed=speed)
print("Voice Generated")
7 changes: 5 additions & 2 deletions apps/api/core-api/app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from app.api import ping
from app.api import ping, main
from app.engine import init_engine

app = FastAPI()

Expand All @@ -19,10 +20,12 @@

@app.on_event("startup")
async def startup():
init_engine()
print("Starting up...")

@app.on_event("shutdown")
async def shutdown():
print("Shutting down...")

app.include_router(ping.router)
app.include_router(ping.router)
app.include_router(main.router)
Empty file.
4 changes: 3 additions & 1 deletion apps/api/core-api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
fastapi==0.111.0
uvicorn==0.29.0
uvicorn==0.29.0
langchain==0.2.1
langchain_openai==0.1.7
9 changes: 8 additions & 1 deletion apps/web/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
const nextConfig = {
images: {
domains: ["localhost"],
}
},
transpilePackages: [
'@ant-design',
"rc-util",
"rc-pagination",
"rc-picker",
"rc-tree",
"rc-table",]
};

export default nextConfig;
Loading

0 comments on commit a37b491

Please sign in to comment.