cvpfus Codex commited on
Commit
d05b639
·
1 Parent(s): f72f54e

Keep app boot path lightweight

Browse files

Co-authored-by: Codex <codex@openai.com>

Files changed (3) hide show
  1. .gitignore +3 -0
  2. app.py +15 -10
  3. requirements.txt +0 -1
.gitignore CHANGED
@@ -7,3 +7,6 @@ outputs/*.mp3
7
  outputs/*.flac
8
  .gradio/
9
  .env
 
 
 
 
7
  outputs/*.flac
8
  .gradio/
9
  .env
10
+ server.pid
11
+ server.out.log
12
+ server.err.log
app.py CHANGED
@@ -2,14 +2,15 @@ from __future__ import annotations
2
 
3
  import json
4
  import os
 
 
5
  import wave
6
  from pathlib import Path
7
  from typing import Any
8
  from uuid import uuid4
9
 
10
- import requests
11
  from fastapi import Request
12
- from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
13
  from fastapi.staticfiles import StaticFiles
14
  from gradio import Server
15
  from pydantic import BaseModel
@@ -68,9 +69,8 @@ def reader_brain_core(node_type: str, text: str, position: str | None, mode: str
68
  )
69
 
70
  try:
71
- response = requests.post(
72
- f"{LLAMA_CPP_BASE_URL}/chat/completions",
73
- json={
74
  "model": LLAMA_CPP_MODEL,
75
  "messages": [
76
  {
@@ -84,11 +84,16 @@ def reader_brain_core(node_type: str, text: str, position: str | None, mode: str
84
  ],
85
  "temperature": 0.2,
86
  "max_tokens": 180,
87
- },
88
- timeout=45,
 
 
 
 
 
89
  )
90
- response.raise_for_status()
91
- payload = response.json()
92
  narration = payload["choices"][0]["message"]["content"].strip()
93
  return {
94
  "ok": True,
@@ -96,7 +101,7 @@ def reader_brain_core(node_type: str, text: str, position: str | None, mode: str
96
  "model": LLAMA_CPP_MODEL,
97
  "narration": narration,
98
  }
99
- except Exception as exc:
100
  prefix = {
101
  "heading": "Heading. ",
102
  "image": "Image. ",
 
2
 
3
  import json
4
  import os
5
+ import urllib.error
6
+ import urllib.request
7
  import wave
8
  from pathlib import Path
9
  from typing import Any
10
  from uuid import uuid4
11
 
 
12
  from fastapi import Request
13
+ from fastapi.responses import HTMLResponse, JSONResponse
14
  from fastapi.staticfiles import StaticFiles
15
  from gradio import Server
16
  from pydantic import BaseModel
 
69
  )
70
 
71
  try:
72
+ body = json.dumps(
73
+ {
 
74
  "model": LLAMA_CPP_MODEL,
75
  "messages": [
76
  {
 
84
  ],
85
  "temperature": 0.2,
86
  "max_tokens": 180,
87
+ }
88
+ ).encode("utf-8")
89
+ request = urllib.request.Request(
90
+ f"{LLAMA_CPP_BASE_URL}/chat/completions",
91
+ data=body,
92
+ headers={"Content-Type": "application/json"},
93
+ method="POST",
94
  )
95
+ with urllib.request.urlopen(request, timeout=45) as response:
96
+ payload = json.loads(response.read().decode("utf-8"))
97
  narration = payload["choices"][0]["message"]["content"].strip()
98
  return {
99
  "ok": True,
 
101
  "model": LLAMA_CPP_MODEL,
102
  "narration": narration,
103
  }
104
+ except (OSError, urllib.error.URLError, KeyError, json.JSONDecodeError) as exc:
105
  prefix = {
106
  "heading": "Heading. ",
107
  "image": "Image. ",
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  gradio>=5.34.0
2
- requests>=2.32.0
3
  pydantic>=2.7.0
4
  kokoro>=0.9.4
5
  soundfile>=0.12.1
 
1
  gradio>=5.34.0
 
2
  pydantic>=2.7.0
3
  kokoro>=0.9.4
4
  soundfile>=0.12.1