File size: 7,751 Bytes
9124b31
 
 
 
d750996
9124b31
 
 
dbad759
9124b31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbad759
 
 
9124b31
024c3c0
e20783b
024c3c0
 
 
 
 
 
 
 
 
 
f9c133e
024c3c0
 
 
 
 
43603aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
024c3c0
 
 
96a8503
fe80c00
59a7ed6
 
 
 
 
 
 
 
5ef2ea1
4486c65
d26352f
 
 
59a7ed6
 
 
 
 
 
 
9124b31
c6abcdb
 
 
 
 
 
 
 
 
 
 
 
9124b31
59a7ed6
 
 
6a2ef16
59a7ed6
 
 
 
 
 
9124b31
59a7ed6
 
e98b99a
 
c6abcdb
 
 
 
e98b99a
 
 
59a7ed6
e98b99a
f479a7d
b586d36
59a7ed6
 
 
 
9124b31
 
 
 
 
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
import os
import re
import httpx
from fastapi import FastAPI, Request, HTTPException, Security
from fastapi.responses import StreamingResponse, Response
from fastapi.security import APIKeyHeader, APIKeyQuery
from itertools import cycle
import asyncio
import json

# --- Configuration ---
PROXY_API_KEY = os.environ.get("PROXY_API_KEY")
VERTEX_EXPRESS_KEYS_STR = os.environ.get("VERTEX_EXPRESS_KEYS")
VERTEX_EXPRESS_KEYS = [key.strip() for key in VERTEX_EXPRESS_KEYS_STR.split(',')] if VERTEX_EXPRESS_KEYS_STR else []

if not VERTEX_EXPRESS_KEYS:
    raise ValueError("VERTEX_EXPRESS_KEYS environment variable not set or empty.")

# --- Globals ---
app = FastAPI()
project_id_cache = {}
key_rotator = cycle(VERTEX_EXPRESS_KEYS)
key_lock = asyncio.Lock()

# --- API Key Security ---
api_key_query = APIKeyQuery(name="key", auto_error=False)
api_key_header = APIKeyHeader(name="x-goog-api-key", auto_error=False)

async def get_api_key(
    key_query: str = Security(api_key_query),
    key_header: str = Security(api_key_header),
):
    if PROXY_API_KEY:
        if key_query == PROXY_API_KEY:
            return key_query
        if key_header == PROXY_API_KEY:
            return key_header
        raise HTTPException(status_code=401, detail="Invalid or missing API Key")
    else:
        # If no PROXY_API_KEY is set, authentication is skipped
        return None

# --- Project ID Extraction ---
async def get_project_id(key: str):
    if key in project_id_cache:
        return project_id_cache[key]

    url = f"https://aiplatform.googleapis.com/v1/publishers/google/models/gemini-2.6-pro:generateContent?key={key}"
    headers = {'Content-Type': 'application/json'}
    data = '{}'

    async with httpx.AsyncClient() as client:
        try:
            response = await client.post(url, headers=headers, data=data)
            response.raise_for_status()
        except httpx.HTTPStatusError as e:
            if e.response.status_code == 404:
                error_message = e.response.json().get("error", {}).get("message", "")
                match = re.search(r"projects/([^/]+)/locations/", error_message)
                if match:
                    project_id = match.group(1)
                    project_id_cache[key] = project_id
                    return project_id
            raise HTTPException(status_code=500, detail=f"Failed to extract project ID: {e.response.text}")
    
    raise HTTPException(status_code=500, detail="Could not extract project ID from any key.")

# --- Proxy Endpoint ---
@app.post("/v1beta/models/{model_path:path}")
async def proxy(request: Request, model_path: str, api_key: str = Security(get_api_key)):
    async with key_lock:
        express_key = next(key_rotator)
    
    project_id = await get_project_id(express_key)

    raw_request_body = await request.body()
    request_body_to_send = raw_request_body

    try:
        request_json = json.loads(raw_request_body)
        if "gemini-2.0-flash-exp-image-generation" in model_path:
            model_path =  model_path.replace("gemini-2.0-flash-exp-image-generation", "gemini-2.5-flash-image-preview")

        if "generationConfig" not in request_json:
            request_json["generationConfig"] = {}

        # Model-specific request body modification
        if "gemini-2.5-flash-image-preview" in model_path:
                if "generationConfig" in request_json and "thinkingConfig" in request_json.get("generationConfig", {}):
                    del request_json["generationConfig"]["thinkingConfig"]
                    print(request_json["generationConfig"])
                if "generationConfig" in request_json and "responseMimeType" in request_json.get("generationConfig", {}):
                    del request_json["generationConfig"]["responseMimeType"]
                request_json["generationConfig"]
                request_json["generationConfig"]["responseModalities"] = ["TEXT", "IMAGE"]

        # request_json["safetySettings"] = [
        #     {
        #         "category": "HARM_CATEGORY_HATE_SPEECH",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_HARASSMENT",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_IMAGE_HATE",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_IMAGE_DANGEROUS_CONTENT",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_IMAGE_HARASSMENT",
        #         "threshold": "BLOCK_NONE"
        #     },
        #     {
        #         "category": "HARM_CATEGORY_IMAGE_SEXUALLY_EXPLICIT",
        #         "threshold": "BLOCK_NONE"
        #     }
        # ]
        request_body_to_send = json.dumps(request_json).encode('utf-8')
    except json.JSONDecodeError:
            pass # Not a json body, proxy as is

    target_url = f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/global/publishers/google/models/{model_path}?key={express_key}"

    client = httpx.AsyncClient(timeout=None)

    headers_to_proxy = {
        k: v for k, v in request.headers.items()
        if k.lower() not in ['host', 'authorization', 'x-goog-api-key', 'content-length']
    }

    print(request_body_to_send)

    if "streamGenerateContent" in model_path:
        target_url = target_url + "&alt=sse"

    req = client.build_request(
        method=request.method,
        url=target_url,
        headers=headers_to_proxy,
        content=request_body_to_send,
    )
    response = await client.send(req, stream=True)

    if response.status_code != 200:
        try:
            response_data = await response.aread()
            return Response(
                content=response_data,
                status_code=response.status_code,
                headers=dict(response.headers),
            )
        finally:
            await response.aclose()
            await client.aclose()

    if "streamGenerateContent" in model_path:
        async def stream_generator():
            try:
                async for line in response.aiter_lines():
                    print(line)
                    yield f"{line}\n"
            finally:
                await response.aclose()
                await client.aclose()
        
        return StreamingResponse(stream_generator(), media_type=response.headers.get("content-type"))
    else:
        try:
            response_data = await response.aread()
            response_json = json.loads(response_data)

            if 'candidates' in response_json:
                for candidate in response_json.get('candidates', []):
                    if 'content' in candidate and 'parts' in candidate.get('content', {}):
                        candidate['content']['parts'] = [part for part in candidate['content']['parts'] if part]

            modified_response_data = json.dumps(response_json).encode('utf-8')

            return Response(
                content=modified_response_data,
                status_code=response.status_code,
                headers={"content-type":response.headers.get("content-type")},
            )
        finally:
            await response.aclose()
            await client.aclose()

if __name__ == "__main__":
    import uvicorn
    # Hugging Face Spaces run on port 7860
    uvicorn.run(app, host="0.0.0.0", port=7860)