File size: 8,578 Bytes
09b9d00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import json
import httpx
import gradio as gr
import uvicorn
from fastapi import FastAPI, Request, Header, HTTPException, File, UploadFile
from fastapi.responses import JSONResponse, StreamingResponse, FileResponse
import time

# ---------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------
BYTEZ_CHAT_URL   = "https://api.bytez.com/models/v2/openai/v1/chat/completions "
BYTEZ_MODELS_URL = "https://api.bytez.com/models/v2/list/models "
BYTEZ_TTS_URL    = "https://api.bytez.com/models/v2/openai/tts-1-hd"
BYTEZ_STT_URL    = "https://api.bytez.com/models/v2/openai/whisper-1"
BYTEZ_AUTH  = os.getenv("BYTEZ_API_KEY")
LOCAL_API_KEY = os.getenv("LOCAL_API_KEY")

# ---------------------------------------------------------------------
# FastAPI backend
# ---------------------------------------------------------------------
api = FastAPI(title="Bytez → OpenAI Proxy")

def check_key(auth: str | None):
    if not auth or not auth.startswith("Bearer "):
        raise HTTPException(status_code=401, detail="Missing or invalid API key")
    user_key = auth.split("Bearer ")[1].strip()
    if LOCAL_API_KEY and user_key != LOCAL_API_KEY:
        raise HTTPException(status_code=403, detail="Unauthorized API key")

# ---------------------------------------------------------------------
# Root / health
# ---------------------------------------------------------------------
@api.get("/")
def root():
    return {"status": "ok", "message": "Bytez proxy running"}

# ---------------------------------------------------------------------
# /v1/models → must look OpenAI-style
# ---------------------------------------------------------------------
@api.get("/v1/models")
async def models(authorization: str = Header(None)):
    check_key(authorization)
    if not BYTEZ_AUTH:
        raise HTTPException(status_code=500, detail="Server BYTEZ_API_KEY not configured")
    async with httpx.AsyncClient(timeout=30) as c:
        r = await c.get(BYTEZ_MODELS_URL, headers={"Authorization": BYTEZ_AUTH})
    try:
        data = r.json()
    except json.JSONDecodeError:
        raise HTTPException(status_code=502, detail="Upstream returned invalid JSON")
    # Transform Bytez format → OpenAI format
    models_list = [
        {"id": m.get("id") or m.get("name"), "object": "model"}
        for m in (data if isinstance(data, list) else data.get("data", []))
    ]
    return JSONResponse(
        {"object": "list", "data": models_list},
        headers={"Access-Control-Allow-Origin": "*"}
    )

# ---------------------------------------------------------------------
# /v1/chat/completions
# ---------------------------------------------------------------------
@api.post("/v1/chat/completions")
async def chat(request: Request, authorization: str = Header(None)):
    check_key(authorization)
    if not BYTEZ_AUTH:
        raise HTTPException(status_code=500, detail="Server BYTEZ_API_KEY not configured")
    payload = await request.json()
    stream = payload.get("stream", False)
    headers = {
        "Authorization": BYTEZ_AUTH,
        "Content-Type": "application/json",
    }
    async def event_stream():
        async with httpx.AsyncClient(timeout=120) as client:
            async with client.stream("POST", BYTEZ_CHAT_URL, headers=headers, json=payload) as upstream:
                async for line in upstream.aiter_lines():
                    line = line.strip()
                    if not line:
                        continue
                    if line.startswith("data: "):
                        json_str = line[6:]
                    else:
                        json_str = line
                    try:
                        chunk = json.loads(json_str)
                    except json.JSONDecodeError:
                        continue
                    if json_str == "[DONE]":
                        yield "data: [DONE]\n\n"
                        break
                    content = ""
                    if "token" in chunk:
                        content = chunk["token"]
                    elif "choices" in chunk and len(chunk["choices"]) > 0:
                        delta = chunk["choices"][0].get("delta", {})
                        content = delta.get("content", "")
                    elif "text" in chunk:
                        content = chunk["text"]
                    else:
                        content = str(chunk)
                    openai_chunk = {
                        "id": "chatcmpl-proxy-stream",
                        "object": "chat.completion.chunk",
                        "created": int(time.time()),
                        "model": payload.get("model", "unknown"),
                        "choices": [
                            {
                                "index": 0,
                                "delta": {"role": "assistant", "content": content},
                                "finish_reason": None,
                            }
                        ],
                    }
                    yield f"data: {json.dumps(openai_chunk)}\n\n"
        yield "data: [DONE]\n\n"

    if stream:
        return StreamingResponse(
            event_stream(),
            media_type="text/event-stream",
            headers={"Access-Control-Allow-Origin": "*"}
        )
    async with httpx.AsyncClient(timeout=120) as c:
        r = await c.post(BYTEZ_CHAT_URL, headers=headers, json=payload)
    try:
        data = r.json()
    except json.JSONDecodeError:
        raise HTTPException(status_code=502, detail="Upstream returned invalid JSON")
    if "choices" not in data:
        content = (
            data.get("output")
            or data.get("response")
            or data.get("message")
            or str(data)
        )
        data = {
            "id": "chatcmpl-proxy",
            "object": "chat.completion",
            "choices": [
                {"index": 0, "message": {"role": "assistant", "content": content}}
            ],
        }
    return JSONResponse(data, headers={"Access-Control-Allow-Origin": "*"})

# ---------------------------------------------------------------------
# /v1/tts (Text-to-Speech)
# ---------------------------------------------------------------------
@api.post("/v1/tts")
async def tts(request: Request, authorization: str = Header(None)):
    check_key(authorization)
    if not BYTEZ_AUTH:
        raise HTTPException(status_code=500, detail="Server BYTEZ_API_KEY not configured")
    payload = await request.json()
    text = payload.get("text", "")
    if not text:
        raise HTTPException(status_code=400, detail="Text not provided for TTS")
    headers = {
        "Authorization": BYTEZ_AUTH,
        "Content-Type": "application/json",
    }
    async with httpx.AsyncClient(timeout=120) as c:
        r = await c.post(BYTEZ_TTS_URL, headers=headers, json={"text": text})
    if r.status_code != 200:
        raise HTTPException(status_code=r.status_code, detail="TTS request failed")
    audio_data = r.content
    return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")

# ---------------------------------------------------------------------
# /v1/stt (Speech-to-Text)
# ---------------------------------------------------------------------
@api.post("/v1/stt")
async def stt(file: UploadFile = File(...), authorization: str = Header(None)):
    check_key(authorization)
    if not BYTEZ_AUTH:
        raise HTTPException(status_code=500, detail="Server BYTEZ_API_KEY not configured")
    headers = {"Authorization": BYTEZ_AUTH}
    file_content = await file.read()
    files = {"file": (file.filename, file_content, file.content_type)}

    async with httpx.AsyncClient(timeout=120) as c:
        r = await c.post(BYTEZ_STT_URL, headers=headers, files=files)

    try:
        data = r.json()
    except json.JSONDecodeError:
        raise HTTPException(status_code=502, detail="Upstream returned invalid JSON")

    transcript = data.get("transcription", "")
    return {"transcription": transcript}

# ---------------------------------------------------------------------
# Minimal Gradio UI (to make HF Space start)
# ---------------------------------------------------------------------
with gr.Blocks() as ui:
    gr.Markdown("### ✅ Jwero Bytez → OpenAI Proxy\n"
                "Endpoints: `/v1/models`, `/v1/chat/completions`, `/v1/tts`, `/v1/stt`")

demo = gr.mount_gradio_app(api, ui, path="/")

if __name__ == "__main__":
    uvicorn.run(demo, host="0.0.0.0", port=7860)