bibibi12345 commited on
Commit
91950c6
·
verified ·
1 Parent(s): c30b9fb

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +27 -9
app/main.py CHANGED
@@ -1,5 +1,6 @@
1
  from fastapi import FastAPI, HTTPException, Depends, Header, Request
2
  from fastapi.responses import JSONResponse, StreamingResponse
 
3
  from fastapi.security import APIKeyHeader
4
  from pydantic import BaseModel, ConfigDict, Field
5
  from typing import List, Dict, Any, Optional, Union, Literal
@@ -23,6 +24,15 @@ client = None
23
 
24
  app = FastAPI(title="OpenAI to Gemini Adapter")
25
 
 
 
 
 
 
 
 
 
 
26
  # API Key security scheme
27
  api_key_header = APIKeyHeader(name="Authorization", auto_error=False)
28
 
@@ -1064,15 +1074,23 @@ async def chat_completions(request: OpenAIRequest, api_key: str = Depends(get_ap
1064
  text_content = part.text
1065
  break
1066
 
1067
- # If we found text content and it's not empty, the response is valid
1068
- if text_content:
1069
- return True
1070
-
1071
- # If no text content was found, check if there are other parts that might be valid
1072
- if hasattr(candidate, 'content') and hasattr(candidate.content, 'parts'):
1073
- if len(candidate.content.parts) > 0:
1074
- # Consider valid if there are any parts at all
1075
- return True
 
 
 
 
 
 
 
 
1076
 
1077
  # Also check if the response itself has text
1078
  if hasattr(response, 'text') and response.text:
 
1
  from fastapi import FastAPI, HTTPException, Depends, Header, Request
2
  from fastapi.responses import JSONResponse, StreamingResponse
3
+ from fastapi.middleware.cors import CORSMiddleware # Import CORS middleware
4
  from fastapi.security import APIKeyHeader
5
  from pydantic import BaseModel, ConfigDict, Field
6
  from typing import List, Dict, Any, Optional, Union, Literal
 
24
 
25
  app = FastAPI(title="OpenAI to Gemini Adapter")
26
 
27
+ # Add CORS middleware to handle preflight OPTIONS requests
28
+ app.add_middleware(
29
+ CORSMiddleware,
30
+ allow_origins=["*"], # Allows all origins
31
+ allow_credentials=True,
32
+ allow_methods=["*"], # Allows all methods (GET, POST, OPTIONS, etc.)
33
+ allow_headers=["*"], # Allows all headers
34
+ )
35
+
36
  # API Key security scheme
37
  api_key_header = APIKeyHeader(name="Authorization", auto_error=False)
38
 
 
1074
  text_content = part.text
1075
  break
1076
 
1077
+ # Check the extracted text content
1078
+ if text_content is None:
1079
+ # No text content was found at all. Check for other parts as a fallback?
1080
+ # For now, let's consider no text as invalid for retry purposes,
1081
+ # as the primary goal is text generation.
1082
+ # If other non-text parts WERE valid outcomes, this logic would need adjustment.
1083
+ # Original check considered any parts as valid if text was missing/empty:
1084
+ # if hasattr(candidate, 'content') and hasattr(candidate.content, 'parts'):
1085
+ # if len(candidate.content.parts) > 0:
1086
+ # return True
1087
+ return False # Treat no text found as invalid
1088
+ elif text_content == '':
1089
+ # Explicit empty string found
1090
+ return False # Treat empty string as invalid for retry
1091
+ else:
1092
+ # Non-empty text content found
1093
+ return True # Valid response
1094
 
1095
  # Also check if the response itself has text
1096
  if hasattr(response, 'text') and response.text: