Skip to content

Commit a37b491

Browse files
committed
Add Redux and Podcast API
1 parent cef1a6d commit a37b491

File tree

22 files changed

+1597
-12
lines changed

22 files changed

+1597
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ yarn-error.log*
3838
*.pem
3939
.DS_Store
4040

41-
__pycache__
41+
__pycache__
42+
tmp

LOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
Run with docker
3+
```sh
4+
docker build -t lerm0/core . --platform=linux/amd64
5+
docker run -it -v /Users/LermoAI/apps/api/core-api:/app lerm0/core bash
6+
```

apps/api/core-api/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ COPY . .
2424
RUN mkdir /.local && chmod 777 /.local
2525
RUN mkdir /.cache && chmod 777 /.cache
2626
RUN pip install -r requirements.txt
27+
RUN pip install git+https://github.com/myshell-ai/MeloTTS.git
28+
RUN python -m unidic download
2729

2830
EXPOSE 8000
2931

30-
ENTRYPOINT python main.py
32+
# ENTRYPOINT python main.py

apps/api/core-api/app/api/main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastapi import APIRouter, HTTPException, Depends, Header, Request
2+
from fastapi.responses import StreamingResponse, FileResponse
3+
4+
from app.engine import create_podcast_script, text_to_voice
5+
6+
router = APIRouter()
7+
8+
@router.post("/podcast")
9+
async def chat_with_model(request: Request):
10+
try:
11+
params = await request.json()
12+
prompt = params.get("prompt")
13+
content = params.get("content")
14+
text = create_podcast_script(content)
15+
16+
prompt_array = prompt.split(", ")[:2]
17+
file_name = prompt_array[0]
18+
speaker = prompt_array[1]
19+
file_path_full = f"/tmp/voice/{file_name}.wav"
20+
21+
text_to_voice(text, file_path_full, speaker)
22+
return FileResponse(path=file_path_full, media_type="audio/wav", filename=f"{file_name}.wav")
23+
except Exception as e:
24+
raise HTTPException(status_code=500, detail=str(e))

apps/api/core-api/app/engine.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from langchain_openai import ChatOpenAI
2+
from melo.api import TTS
3+
4+
5+
llm = None
6+
VOICE_ENGINE = None
7+
device = 'cpu'
8+
def init_engine():
9+
global llm
10+
global VOICE_ENGINE
11+
if not llm:
12+
llm = ChatOpenAI(model="gpt4", temperature=0)
13+
if not VOICE_ENGINE:
14+
VOICE_ENGINE = TTS(language='EN', device=device)
15+
16+
def create_podcast_script(content: str):
17+
prompt = """
18+
Go deep into {content} and explain like human talk max 300 words.
19+
"""
20+
result = llm.invoke(prompt.format(content=content))
21+
return result.content
22+
23+
def text_to_voice(text: str, file_path: str, speaker: str):
24+
speed = 1
25+
speaker_ids = VOICE_ENGINE.hps.data.spk2id
26+
VOICE_ENGINE.tts_to_file(text, speaker_ids[speaker], file_path, speed=speed)
27+
print("Voice Generated")

apps/api/core-api/app/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import FastAPI
22
from starlette.middleware.cors import CORSMiddleware
3-
from app.api import ping
3+
from app.api import ping, main
4+
from app.engine import init_engine
45

56
app = FastAPI()
67

@@ -19,10 +20,12 @@
1920

2021
@app.on_event("startup")
2122
async def startup():
23+
init_engine()
2224
print("Starting up...")
2325

2426
@app.on_event("shutdown")
2527
async def shutdown():
2628
print("Shutting down...")
2729

28-
app.include_router(ping.router)
30+
app.include_router(ping.router)
31+
app.include_router(main.router)

apps/api/core-api/app/template.py

Whitespace-only changes.

apps/api/core-api/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
fastapi==0.111.0
2-
uvicorn==0.29.0
2+
uvicorn==0.29.0
3+
langchain==0.2.1
4+
langchain_openai==0.1.7

apps/web/next.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
const nextConfig = {
33
images: {
44
domains: ["localhost"],
5-
}
5+
},
6+
transpilePackages: [
7+
'@ant-design',
8+
"rc-util",
9+
"rc-pagination",
10+
"rc-picker",
11+
"rc-tree",
12+
"rc-table",]
613
};
714

815
export default nextConfig;

0 commit comments

Comments
 (0)