clash-linux commited on
Commit
21ee4ec
·
verified ·
1 Parent(s): 0bcee74

Upload 8 files

Browse files
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install dependencies
6
+ COPY app/requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # Copy application code
10
+ COPY app/ .
11
+
12
+ # Create a directory for the credentials
13
+ RUN mkdir -p /app/credentials
14
+
15
+ # Expose the port
16
+ EXPOSE 8050
17
+
18
+ # Command to run the application
19
+ # Use the default Hugging Face port 7860
20
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2025 gzzhongqi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,11 +1,203 @@
1
- ---
2
- title: Vertex Fast
3
- emoji: 📊
4
- colorFrom: blue
5
- colorTo: gray
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: OpenAI to Gemini Adapter
3
+ emoji: 🔄☁️
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ app_port: 7860
8
+ ---
9
+
10
+ # OpenAI to Gemini Adapter
11
+
12
+ This service provides an OpenAI-compatible API that translates requests to Google's Vertex AI Gemini models, allowing you to use Gemini models with tools expecting an OpenAI interface.
13
+
14
+ ## Features
15
+
16
+ - OpenAI-compatible API endpoints (`/v1/chat/completions`, `/v1/models`).
17
+ - Supports Google Cloud credentials via `GOOGLE_CREDENTIALS_JSON` secret (recommended for Spaces) or local file methods.
18
+ - Supports credential rotation when using local files.
19
+ - Handles streaming and non-streaming responses.
20
+ - Configured for easy deployment on Hugging Face Spaces using Docker (port 7860) or locally via Docker Compose (port 8050).
21
+
22
+ ## Hugging Face Spaces Deployment (Recommended)
23
+
24
+ This application is ready for deployment on Hugging Face Spaces using Docker.
25
+
26
+ 1. **Create a new Space:** Go to Hugging Face Spaces and create a new Space, choosing "Docker" as the Space SDK.
27
+ 2. **Upload Files:** Upload the `app/` directory, `Dockerfile`, and `app/requirements.txt` to your Space repository. You can do this via the web interface or using Git.
28
+ 3. **Configure Secrets:** In your Space settings, navigate to the **Secrets** section and add the following secrets:
29
+ * `API_KEY`: Your desired API key for authenticating requests to this adapter service. If not set, it defaults to `123456`.
30
+ * `GOOGLE_CREDENTIALS_JSON`: The **entire content** of your Google Cloud service account JSON key file. Copy and paste the JSON content directly into the secret value field. **This is the required method for providing credentials on Hugging Face.**
31
+ 4. **Deployment:** Hugging Face will automatically build and deploy the Docker container. The application will run on port 7860 as defined in the `Dockerfile` and this README's metadata.
32
+
33
+ Your adapter service will be available at the URL provided by your Hugging Face Space (e.g., `https://your-user-name-your-space-name.hf.space`).
34
+
35
+ ## Local Docker Setup (for Development/Testing)
36
+
37
+ ### Prerequisites
38
+
39
+ - Docker and Docker Compose
40
+ - Google Cloud service account credentials with Vertex AI access
41
+
42
+ ### Credential Setup (Local Docker)
43
+
44
+ 1. Create a `credentials` directory in the project root:
45
+ ```bash
46
+ mkdir -p credentials
47
+ ```
48
+ 2. Add your service account JSON files to the `credentials` directory:
49
+ ```bash
50
+ # Example with multiple credential files
51
+ cp /path/to/your/service-account1.json credentials/service-account1.json
52
+ cp /path/to/your/service-account2.json credentials/service-account2.json
53
+ ```
54
+ The service will automatically detect and rotate through all `.json` files in this directory if the `GOOGLE_CREDENTIALS_JSON` environment variable is *not* set.
55
+ 3. Alternatively, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable *in your local environment or `docker-compose.yml`* to the *path* of a single credential file (used as a fallback if the other methods fail).
56
+
57
+ ### Running Locally
58
+
59
+ Start the service using Docker Compose:
60
+
61
+ ```bash
62
+ docker-compose up -d
63
+ ```
64
+
65
+ The service will be available at `http://localhost:8050` (as defined in `docker-compose.yml`).
66
+
67
+ ## API Usage
68
+
69
+ The service implements OpenAI-compatible endpoints:
70
+
71
+ - `GET /v1/models` - List available models
72
+ - `POST /v1/chat/completions` - Create a chat completion
73
+ - `GET /health` - Health check endpoint (includes credential status)
74
+
75
+ All endpoints require authentication using an API key in the Authorization header.
76
+
77
+ ### Authentication
78
+
79
+ The service requires an API key for authentication.
80
+
81
+ To authenticate, include the API key in the `Authorization` header using the `Bearer` token format:
82
+
83
+ ```
84
+ Authorization: Bearer YOUR_API_KEY
85
+ ```
86
+
87
+ Replace `YOUR_API_KEY` with the key you configured (either via the `API_KEY` secret/environment variable or the default `123456`).
88
+
89
+ ### Example Requests
90
+
91
+ *(Replace `YOUR_ADAPTER_URL` with your Hugging Face Space URL or `http://localhost:8050` if running locally)*
92
+
93
+ #### Basic Request
94
+
95
+ ```bash
96
+ curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
97
+ -H "Content-Type: application/json" \
98
+ -H "Authorization: Bearer YOUR_API_KEY" \
99
+ -d '{
100
+ "model": "gemini-1.5-pro",
101
+ "messages": [
102
+ {"role": "system", "content": "You are a helpful assistant."},
103
+ {"role": "user", "content": "Hello, how are you?"}
104
+ ],
105
+ "temperature": 0.7
106
+ }'
107
+ ```
108
+
109
+ #### Grounded Search Request
110
+
111
+ ```bash
112
+ curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
113
+ -H "Content-Type: application/json" \
114
+ -H "Authorization: Bearer YOUR_API_KEY" \
115
+ -d '{
116
+ "model": "gemini-2.5-pro-exp-03-25-search",
117
+ "messages": [
118
+ {"role": "system", "content": "You are a helpful assistant with access to the latest information."},
119
+ {"role": "user", "content": "What are the latest developments in quantum computing?"}
120
+ ],
121
+ "temperature": 0.2
122
+ }'
123
+ ```
124
+
125
+ ### Supported Models
126
+
127
+ The API supports the following Vertex AI Gemini models:
128
+
129
+ | Model ID | Description |
130
+ | ------------------------------ | ---------------------------------------------- |
131
+ | `gemini-2.5-pro-exp-03-25` | Gemini 2.5 Pro Experimental (March 25) |
132
+ | `gemini-2.5-pro-exp-03-25-search` | Gemini 2.5 Pro with Google Search grounding |
133
+ | `gemini-2.0-flash` | Gemini 2.0 Flash |
134
+ | `gemini-2.0-flash-search` | Gemini 2.0 Flash with Google Search grounding |
135
+ | `gemini-2.0-flash-lite` | Gemini 2.0 Flash Lite |
136
+ | `gemini-2.0-flash-lite-search` | Gemini 2.0 Flash Lite with Google Search grounding |
137
+ | `gemini-2.0-pro-exp-02-05` | Gemini 2.0 Pro Experimental (February 5) |
138
+ | `gemini-1.5-flash` | Gemini 1.5 Flash |
139
+ | `gemini-1.5-flash-8b` | Gemini 1.5 Flash 8B |
140
+ | `gemini-1.5-pro` | Gemini 1.5 Pro |
141
+ | `gemini-1.0-pro-002` | Gemini 1.0 Pro |
142
+ | `gemini-1.0-pro-vision-001` | Gemini 1.0 Pro Vision |
143
+ | `gemini-embedding-exp` | Gemini Embedding Experimental |
144
+
145
+ Models with the `-search` suffix enable grounding with Google Search using dynamic retrieval.
146
+
147
+ ### Supported Parameters
148
+
149
+ The API supports common OpenAI-compatible parameters, mapping them to Vertex AI where possible:
150
+
151
+ | OpenAI Parameter | Vertex AI Parameter | Description |
152
+ | ------------------- | --------------------- | ------------------------------------------------- |
153
+ | `temperature` | `temperature` | Controls randomness (0.0 to 1.0) |
154
+ | `max_tokens` | `max_output_tokens` | Maximum number of tokens to generate |
155
+ | `top_p` | `top_p` | Nucleus sampling parameter (0.0 to 1.0) |
156
+ | `top_k` | `top_k` | Top-k sampling parameter |
157
+ | `stop` | `stop_sequences` | List of strings that stop generation when encountered |
158
+ | `presence_penalty` | `presence_penalty` | Penalizes repeated tokens |
159
+ | `frequency_penalty` | `frequency_penalty` | Penalizes frequent tokens |
160
+ | `seed` | `seed` | Random seed for deterministic generation |
161
+ | `logprobs` | `logprobs` | Number of log probabilities to return |
162
+ | `n` | `candidate_count` | Number of completions to generate |
163
+
164
+ ## Credential Handling Priority
165
+
166
+ The application loads Google Cloud credentials in the following order:
167
+
168
+ 1. **`GOOGLE_CREDENTIALS_JSON` Environment Variable / Secret:** Checks for the JSON *content* directly in this variable (Required for Hugging Face).
169
+ 2. **`credentials/` Directory (Local Only):** Looks for `.json` files in the directory specified by `CREDENTIALS_DIR` (Default: `/app/credentials` inside the container). Rotates through found files. Used if `GOOGLE_CREDENTIALS_JSON` is not set.
170
+ 3. **`GOOGLE_APPLICATION_CREDENTIALS` Environment Variable (Local Only):** Checks for a *file path* specified by this variable. Used as a fallback if the above methods fail.
171
+
172
+ ## Environment Variables / Secrets
173
+
174
+ - `API_KEY`: API key for authentication (Default: `123456`). **Required as Secret on Hugging Face.**
175
+ - `GOOGLE_CREDENTIALS_JSON`: **(Required Secret on Hugging Face)** The full JSON content of your service account key. Takes priority over other methods.
176
+ - `CREDENTIALS_DIR` (Local Only): Directory containing credential files (Default: `/app/credentials` in the container). Used if `GOOGLE_CREDENTIALS_JSON` is not set.
177
+ - `GOOGLE_APPLICATION_CREDENTIALS` (Local Only): Path to a *specific* credential file. Used as a fallback if the above methods fail.
178
+ - `PORT`: Not needed for `CMD` config (uses 7860). Hugging Face provides this automatically, `docker-compose.yml` maps 8050 locally.
179
+
180
+ ## Health Check
181
+
182
+ You can check the status of the service using the health endpoint:
183
+
184
+ ```bash
185
+ curl YOUR_ADAPTER_URL/health -H "Authorization: Bearer YOUR_API_KEY"
186
+ ```
187
+
188
+ This returns information about the credential status:
189
+
190
+ ```json
191
+ {
192
+ "status": "ok",
193
+ "credentials": {
194
+ "available": 1, // Example: 1 if loaded via JSON secret, or count if loaded from files
195
+ "files": [], // Lists files only if using CREDENTIALS_DIR method
196
+ "current_index": 0
197
+ }
198
+ }
199
+ ```
200
+
201
+ ## License
202
+
203
+ This project is licensed under the MIT License.
app/config.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Default password if not set in environment
4
+ DEFAULT_PASSWORD = "123456"
5
+
6
+ # Get password from environment variable or use default
7
+ API_KEY = os.environ.get("API_KEY", DEFAULT_PASSWORD)
8
+
9
+ # Function to validate API key
10
+ def validate_api_key(api_key: str) -> bool:
11
+ """
12
+ Validate the provided API key against the configured key
13
+
14
+ Args:
15
+ api_key: The API key to validate
16
+
17
+ Returns:
18
+ bool: True if the key is valid, False otherwise
19
+ """
20
+ if not API_KEY:
21
+ # If no API key is configured, authentication is disabled
22
+ return True
23
+
24
+ return api_key == API_KEY
app/main.py ADDED
The diff for this file is too large to render. See raw diff
 
app/requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.110.0
2
+ uvicorn==0.27.1
3
+ google-auth==2.38.0
4
+ google-cloud-aiplatform==1.86.0
5
+ pydantic==2.6.1
6
+ google-genai==1.13.0
7
+ openai
credentials/Placeholder Place credential json files here ADDED
File without changes
docker-compose.yml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ openai-to-gemini:
5
+ build:
6
+ context: .
7
+ dockerfile: Dockerfile
8
+ ports:
9
+ # Map host port 8050 to container port 7860 (for Hugging Face compatibility)
10
+ - "8050:7860"
11
+ volumes:
12
+ - ./credentials:/app/credentials
13
+ environment:
14
+ # This is kept for backward compatibility but our app now primarily uses the credential manager
15
+ - GOOGLE_APPLICATION_CREDENTIALS=/app/credentials/service-account.json
16
+ # Directory where credential files are stored (used by credential manager)
17
+ - CREDENTIALS_DIR=/app/credentials
18
+ # API key for authentication (default: 123456)
19
+ - API_KEY=123456
20
+ # Enable/disable fake streaming (default: false)
21
+ - FAKE_STREAMING=false
22
+ # Interval for fake streaming keep-alive messages (default: 1.0)
23
+ - FAKE_STREAMING_INTERVAL=1.0
24
+ restart: unless-stopped