Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -22,8 +22,9 @@ API_KEYS = [
|
|
| 22 |
]
|
| 23 |
|
| 24 |
BASE_URL = os.getenv("BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/")
|
| 25 |
-
EXPECTED_API_KEY = os.getenv("
|
| 26 |
API_KEY_NAME = "Authorization"
|
|
|
|
| 27 |
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
| 28 |
app = FastAPI(title="OpenAI-SDK-compatible API", version="1.0.0", description="Un wrapper FastAPI compatibile con le specifiche dell'API OpenAI.")
|
| 29 |
app.add_middleware(
|
|
@@ -34,17 +35,7 @@ app.add_middleware(
|
|
| 34 |
allow_headers=["*"],
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
role: str
|
| 39 |
-
content: str
|
| 40 |
-
|
| 41 |
-
class ChatCompletionRequest(BaseModel):
|
| 42 |
-
model: str = "gemini-2.0-flash"
|
| 43 |
-
messages: List[Message]
|
| 44 |
-
max_tokens: Optional[int] = 8196
|
| 45 |
-
temperature: Optional[float] = 0.8
|
| 46 |
-
stream: Optional[bool] = False
|
| 47 |
-
|
| 48 |
def verify_api_key(api_key: str = Depends(api_key_header)):
|
| 49 |
if not api_key:
|
| 50 |
raise HTTPException(status_code=403, detail="API key mancante")
|
|
@@ -94,10 +85,27 @@ async def _resp_async_generator(params: ChatCompletionRequest):
|
|
| 94 |
error_data = {"error": str(e)}
|
| 95 |
yield f"data: {json.dumps(error_data)}\n\n"
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
@app.get("/health")
|
| 98 |
async def health_check():
|
| 99 |
return {"message": "success"}
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
@app.post("/v1/chat/completions", dependencies=[Depends(verify_api_key)])
|
| 102 |
async def chat_completions(req: ChatCompletionRequest):
|
| 103 |
if not req.messages:
|
|
@@ -112,8 +120,4 @@ async def chat_completions(req: ChatCompletionRequest):
|
|
| 112 |
response = call_api_sync(req)
|
| 113 |
return response
|
| 114 |
except Exception as e:
|
| 115 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 116 |
-
|
| 117 |
-
@app.get("/")
|
| 118 |
-
def read_general():
|
| 119 |
-
return {"response": "Benvenuto"}
|
|
|
|
| 22 |
]
|
| 23 |
|
| 24 |
BASE_URL = os.getenv("BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/")
|
| 25 |
+
EXPECTED_API_KEY = os.getenv("API_HUGGINGFACE")
|
| 26 |
API_KEY_NAME = "Authorization"
|
| 27 |
+
|
| 28 |
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
| 29 |
app = FastAPI(title="OpenAI-SDK-compatible API", version="1.0.0", description="Un wrapper FastAPI compatibile con le specifiche dell'API OpenAI.")
|
| 30 |
app.add_middleware(
|
|
|
|
| 35 |
allow_headers=["*"],
|
| 36 |
)
|
| 37 |
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def verify_api_key(api_key: str = Depends(api_key_header)):
|
| 40 |
if not api_key:
|
| 41 |
raise HTTPException(status_code=403, detail="API key mancante")
|
|
|
|
| 85 |
error_data = {"error": str(e)}
|
| 86 |
yield f"data: {json.dumps(error_data)}\n\n"
|
| 87 |
|
| 88 |
+
# ---------------------------------------------------------------------------------------
|
| 89 |
+
@app.get("/")
|
| 90 |
+
def read_general():
|
| 91 |
+
return {"response": "Benvenuto"}
|
| 92 |
+
|
| 93 |
@app.get("/health")
|
| 94 |
async def health_check():
|
| 95 |
return {"message": "success"}
|
| 96 |
|
| 97 |
+
class Message(BaseModel):
|
| 98 |
+
role: str
|
| 99 |
+
content: str
|
| 100 |
+
|
| 101 |
+
# ---------------------------------- Generazione Testo ---------------------------------------
|
| 102 |
+
class ChatCompletionRequest(BaseModel):
|
| 103 |
+
model: str = "gemini-2.0-flash"
|
| 104 |
+
messages: List[Message]
|
| 105 |
+
max_tokens: Optional[int] = 8196
|
| 106 |
+
temperature: Optional[float] = 0.8
|
| 107 |
+
stream: Optional[bool] = False
|
| 108 |
+
|
| 109 |
@app.post("/v1/chat/completions", dependencies=[Depends(verify_api_key)])
|
| 110 |
async def chat_completions(req: ChatCompletionRequest):
|
| 111 |
if not req.messages:
|
|
|
|
| 120 |
response = call_api_sync(req)
|
| 121 |
return response
|
| 122 |
except Exception as e:
|
| 123 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|