Spaces:
Running
Running
Commit
·
22b52c3
1
Parent(s):
8b7abe5
hf support
Browse files- app/main.py +142 -36
- docker-compose.yml +19 -19
app/main.py
CHANGED
@@ -179,38 +179,87 @@ def init_vertex_ai():
|
|
179 |
credentials_json_str = os.environ.get("GOOGLE_CREDENTIALS_JSON")
|
180 |
if credentials_json_str:
|
181 |
try:
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
return True
|
188 |
except Exception as e:
|
189 |
print(f"Error loading credentials from GOOGLE_CREDENTIALS_JSON: {e}")
|
190 |
# Fall through to other methods if this fails
|
191 |
|
192 |
# Priority 2: Try to use the credential manager to get credentials from files
|
|
|
193 |
credentials, project_id = credential_manager.get_next_credentials()
|
194 |
|
195 |
if credentials and project_id:
|
196 |
-
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
197 |
-
print(f"Initialized Vertex AI using Credential Manager for project: {project_id}")
|
198 |
-
return True
|
199 |
-
|
200 |
-
# Priority 3: Fall back to GOOGLE_APPLICATION_CREDENTIALS environment variable (file path)
|
201 |
-
file_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
202 |
-
if file_path and os.path.exists(file_path):
|
203 |
try:
|
204 |
-
credentials = service_account.Credentials.from_service_account_file(file_path, scopes=['https://www.googleapis.com/auth/cloud-platform'])
|
205 |
-
project_id = credentials.project_id
|
206 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
207 |
-
print(f"Initialized Vertex AI using
|
208 |
return True
|
209 |
except Exception as e:
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
# If none of the methods worked
|
213 |
-
print(f"
|
214 |
return False
|
215 |
except Exception as e:
|
216 |
print(f"Error initializing authentication: {e}")
|
@@ -651,25 +700,15 @@ async def chat_completions(request: OpenAIRequest, api_key: str = Depends(get_ap
|
|
651 |
# Create generation config
|
652 |
generation_config = create_generation_config(request)
|
653 |
|
654 |
-
#
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
# Initialize Vertex AI with the rotated credentials
|
664 |
-
try:
|
665 |
-
# Re-initialize client for this request - credentials might have rotated
|
666 |
-
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
667 |
-
print(f"Using credentials for project: {project_id} for this request")
|
668 |
-
except Exception as auth_error:
|
669 |
-
error_response = create_openai_error_response(
|
670 |
-
500, f"Failed to initialize authentication: {str(auth_error)}", "server_error"
|
671 |
-
)
|
672 |
-
return JSONResponse(status_code=500, content=error_response)
|
673 |
|
674 |
# Initialize Gemini model
|
675 |
search_tool = types.Tool(google_search=types.GoogleSearch())
|
@@ -778,3 +817,70 @@ def health_check(api_key: str = Depends(get_api_key)):
|
|
778 |
"current_index": credential_manager.current_index
|
779 |
}
|
780 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
credentials_json_str = os.environ.get("GOOGLE_CREDENTIALS_JSON")
|
180 |
if credentials_json_str:
|
181 |
try:
|
182 |
+
# Try to parse the JSON
|
183 |
+
try:
|
184 |
+
credentials_info = json.loads(credentials_json_str)
|
185 |
+
# Check if the parsed JSON has the expected structure
|
186 |
+
if not isinstance(credentials_info, dict):
|
187 |
+
# print(f"ERROR: Parsed JSON is not a dictionary, type: {type(credentials_info)}") # Removed
|
188 |
+
raise ValueError("Credentials JSON must be a dictionary")
|
189 |
+
# Check for required fields in the service account JSON
|
190 |
+
required_fields = ["type", "project_id", "private_key_id", "private_key", "client_email"]
|
191 |
+
missing_fields = [field for field in required_fields if field not in credentials_info]
|
192 |
+
if missing_fields:
|
193 |
+
# print(f"ERROR: Missing required fields in credentials JSON: {missing_fields}") # Removed
|
194 |
+
raise ValueError(f"Credentials JSON missing required fields: {missing_fields}")
|
195 |
+
except json.JSONDecodeError as json_err:
|
196 |
+
print(f"ERROR: Failed to parse GOOGLE_CREDENTIALS_JSON as JSON: {json_err}")
|
197 |
+
raise
|
198 |
+
|
199 |
+
# Create credentials from the parsed JSON info (json.loads should handle \n)
|
200 |
+
try:
|
201 |
+
|
202 |
+
credentials = service_account.Credentials.from_service_account_info(
|
203 |
+
credentials_info, # Pass the dictionary directly
|
204 |
+
scopes=['https://www.googleapis.com/auth/cloud-platform']
|
205 |
+
)
|
206 |
+
project_id = credentials.project_id
|
207 |
+
print(f"Successfully created credentials object for project: {project_id}")
|
208 |
+
except Exception as cred_err:
|
209 |
+
print(f"ERROR: Failed to create credentials from service account info: {cred_err}")
|
210 |
+
raise
|
211 |
+
|
212 |
+
# Initialize the client with the credentials
|
213 |
+
try:
|
214 |
+
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
215 |
+
print(f"Initialized Vertex AI using GOOGLE_CREDENTIALS_JSON env var for project: {project_id}")
|
216 |
+
except Exception as client_err:
|
217 |
+
print(f"ERROR: Failed to initialize genai.Client: {client_err}")
|
218 |
+
raise
|
219 |
return True
|
220 |
except Exception as e:
|
221 |
print(f"Error loading credentials from GOOGLE_CREDENTIALS_JSON: {e}")
|
222 |
# Fall through to other methods if this fails
|
223 |
|
224 |
# Priority 2: Try to use the credential manager to get credentials from files
|
225 |
+
print(f"Trying credential manager (directory: {credential_manager.credentials_dir})")
|
226 |
credentials, project_id = credential_manager.get_next_credentials()
|
227 |
|
228 |
if credentials and project_id:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
try:
|
|
|
|
|
230 |
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
231 |
+
print(f"Initialized Vertex AI using Credential Manager for project: {project_id}")
|
232 |
return True
|
233 |
except Exception as e:
|
234 |
+
print(f"ERROR: Failed to initialize client with credentials from Credential Manager: {e}")
|
235 |
+
|
236 |
+
# Priority 3: Fall back to GOOGLE_APPLICATION_CREDENTIALS environment variable (file path)
|
237 |
+
file_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
238 |
+
if file_path:
|
239 |
+
print(f"Checking GOOGLE_APPLICATION_CREDENTIALS file path: {file_path}")
|
240 |
+
if os.path.exists(file_path):
|
241 |
+
try:
|
242 |
+
print(f"File exists, attempting to load credentials")
|
243 |
+
credentials = service_account.Credentials.from_service_account_file(
|
244 |
+
file_path,
|
245 |
+
scopes=['https://www.googleapis.com/auth/cloud-platform']
|
246 |
+
)
|
247 |
+
project_id = credentials.project_id
|
248 |
+
print(f"Successfully loaded credentials from file for project: {project_id}")
|
249 |
+
|
250 |
+
try:
|
251 |
+
client = genai.Client(vertexai=True, credentials=credentials, project=project_id, location="us-central1")
|
252 |
+
print(f"Initialized Vertex AI using GOOGLE_APPLICATION_CREDENTIALS file path for project: {project_id}")
|
253 |
+
return True
|
254 |
+
except Exception as client_err:
|
255 |
+
print(f"ERROR: Failed to initialize client with credentials from file: {client_err}")
|
256 |
+
except Exception as e:
|
257 |
+
print(f"ERROR: Failed to load credentials from GOOGLE_APPLICATION_CREDENTIALS path {file_path}: {e}")
|
258 |
+
else:
|
259 |
+
print(f"ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist at path: {file_path}")
|
260 |
|
261 |
# If none of the methods worked
|
262 |
+
print(f"ERROR: No valid credentials found. Tried GOOGLE_CREDENTIALS_JSON, Credential Manager ({credential_manager.credentials_dir}), and GOOGLE_APPLICATION_CREDENTIALS.")
|
263 |
return False
|
264 |
except Exception as e:
|
265 |
print(f"Error initializing authentication: {e}")
|
|
|
700 |
# Create generation config
|
701 |
generation_config = create_generation_config(request)
|
702 |
|
703 |
+
# Use the globally initialized client (from startup)
|
704 |
+
global client
|
705 |
+
if client is None:
|
706 |
+
# This should ideally not happen if startup was successful
|
707 |
+
error_response = create_openai_error_response(
|
708 |
+
500, "Vertex AI client not initialized", "server_error"
|
709 |
+
)
|
710 |
+
return JSONResponse(status_code=500, content=error_response)
|
711 |
+
print(f"Using globally initialized client.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
712 |
|
713 |
# Initialize Gemini model
|
714 |
search_tool = types.Tool(google_search=types.GoogleSearch())
|
|
|
817 |
"current_index": credential_manager.current_index
|
818 |
}
|
819 |
}
|
820 |
+
|
821 |
+
# Diagnostic endpoint for troubleshooting credential issues
|
822 |
+
@app.get("/debug/credentials")
|
823 |
+
def debug_credentials(api_key: str = Depends(get_api_key)):
|
824 |
+
"""
|
825 |
+
Diagnostic endpoint to check credential configuration without actually authenticating.
|
826 |
+
This helps troubleshoot issues with credential setup, especially on Hugging Face.
|
827 |
+
"""
|
828 |
+
# Check GOOGLE_CREDENTIALS_JSON
|
829 |
+
creds_json = os.environ.get("GOOGLE_CREDENTIALS_JSON")
|
830 |
+
creds_json_status = {
|
831 |
+
"present": creds_json is not None,
|
832 |
+
"length": len(creds_json) if creds_json else 0,
|
833 |
+
"parse_status": "not_attempted"
|
834 |
+
}
|
835 |
+
|
836 |
+
# Try to parse the JSON if present
|
837 |
+
if creds_json:
|
838 |
+
try:
|
839 |
+
creds_info = json.loads(creds_json)
|
840 |
+
# Check for required fields
|
841 |
+
required_fields = ["type", "project_id", "private_key_id", "private_key", "client_email"]
|
842 |
+
missing_fields = [field for field in required_fields if field not in creds_info]
|
843 |
+
|
844 |
+
creds_json_status.update({
|
845 |
+
"parse_status": "success",
|
846 |
+
"is_dict": isinstance(creds_info, dict),
|
847 |
+
"missing_required_fields": missing_fields,
|
848 |
+
"project_id": creds_info.get("project_id", "not_found"),
|
849 |
+
# Include a safe sample of the private key to check if it's properly formatted
|
850 |
+
"private_key_sample": creds_info.get("private_key", "not_found")[:10] + "..." if "private_key" in creds_info else "not_found"
|
851 |
+
})
|
852 |
+
except json.JSONDecodeError as e:
|
853 |
+
creds_json_status.update({
|
854 |
+
"parse_status": "error",
|
855 |
+
"error": str(e),
|
856 |
+
"sample": creds_json[:20] + "..." if len(creds_json) > 20 else creds_json
|
857 |
+
})
|
858 |
+
|
859 |
+
# Check credential files
|
860 |
+
credential_manager.refresh_credentials_list()
|
861 |
+
|
862 |
+
# Check GOOGLE_APPLICATION_CREDENTIALS
|
863 |
+
app_creds_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
|
864 |
+
app_creds_status = {
|
865 |
+
"present": app_creds_path is not None,
|
866 |
+
"path": app_creds_path,
|
867 |
+
"exists": os.path.exists(app_creds_path) if app_creds_path else False
|
868 |
+
}
|
869 |
+
|
870 |
+
return {
|
871 |
+
"environment": {
|
872 |
+
"GOOGLE_CREDENTIALS_JSON": creds_json_status,
|
873 |
+
"CREDENTIALS_DIR": {
|
874 |
+
"path": credential_manager.credentials_dir,
|
875 |
+
"exists": os.path.exists(credential_manager.credentials_dir),
|
876 |
+
"files_found": len(credential_manager.credentials_files),
|
877 |
+
"files": [os.path.basename(f) for f in credential_manager.credentials_files]
|
878 |
+
},
|
879 |
+
"GOOGLE_APPLICATION_CREDENTIALS": app_creds_status
|
880 |
+
},
|
881 |
+
"recommendations": [
|
882 |
+
"Ensure GOOGLE_CREDENTIALS_JSON contains the full, properly formatted JSON content of your service account key",
|
883 |
+
"Check for any special characters or line breaks that might need proper escaping",
|
884 |
+
"Verify that the service account has the necessary permissions for Vertex AI"
|
885 |
+
]
|
886 |
+
}
|
docker-compose.yml
CHANGED
@@ -1,20 +1,20 @@
|
|
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 |
restart: unless-stopped
|
|
|
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 |
restart: unless-stopped
|