Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -55,6 +55,14 @@ class ChatCompletionRequest(BaseModel):
|
|
55 |
class Config:
|
56 |
extra = "allow"
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Server FAST API
|
59 |
app = FastAPI(title="OpenAI-SDK-compatible API", version="1.0.0", description="Un wrapper FastAPI compatibile con le specifiche dell'API OpenAI.")
|
60 |
app.add_middleware(
|
@@ -361,7 +369,7 @@ def convert_format(audio_bytes: bytes, from_fmt: str, to_fmt: str) -> bytes:
|
|
361 |
audio.export(buf, format=to_fmt)
|
362 |
return buf.getvalue()
|
363 |
|
364 |
-
def parse_audio_mime_type(mime_type: str):
|
365 |
"""Parses bits per sample and rate from an audio MIME type string """
|
366 |
bits_per_sample = 16
|
367 |
rate = 24000
|
@@ -471,6 +479,12 @@ async def chat_completions(req: ChatCompletionRequest):
|
|
471 |
try:
|
472 |
if not req.messages:
|
473 |
raise HTTPException(status_code=400, detail="Nessun messaggio fornito")
|
|
|
|
|
|
|
|
|
|
|
|
|
474 |
if not req.stream:
|
475 |
return call_api_sync(req)
|
476 |
else:
|
@@ -478,6 +492,18 @@ async def chat_completions(req: ChatCompletionRequest):
|
|
478 |
except Exception as e:
|
479 |
raise HTTPException(status_code=500, detail=str(e))
|
480 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
481 |
@app.post("/v1/audio/transcriptions", dependencies=[Depends(verify_api_key)])
|
482 |
async def audio_transcriptions_endpoint(
|
483 |
file: UploadFile = File(...),
|
|
|
55 |
class Config:
|
56 |
extra = "allow"
|
57 |
|
58 |
+
class EmbeddingsRequest(BaseModel):
|
59 |
+
model: str = "text-embedding-004"
|
60 |
+
input: Any
|
61 |
+
encoding_format: Optional[str] = None
|
62 |
+
user: Optional[str] = None
|
63 |
+
class Config:
|
64 |
+
extra = "allow"
|
65 |
+
|
66 |
# Server FAST API
|
67 |
app = FastAPI(title="OpenAI-SDK-compatible API", version="1.0.0", description="Un wrapper FastAPI compatibile con le specifiche dell'API OpenAI.")
|
68 |
app.add_middleware(
|
|
|
369 |
audio.export(buf, format=to_fmt)
|
370 |
return buf.getvalue()
|
371 |
|
372 |
+
def parse_audio_mime_type(mime_type: str) -> dict[str, int | None]:
|
373 |
"""Parses bits per sample and rate from an audio MIME type string """
|
374 |
bits_per_sample = 16
|
375 |
rate = 24000
|
|
|
479 |
try:
|
480 |
if not req.messages:
|
481 |
raise HTTPException(status_code=400, detail="Nessun messaggio fornito")
|
482 |
+
# Elimino stop altrimenti dà errore per Gemini
|
483 |
+
if getattr(req, "stop", "__missing__") is None:
|
484 |
+
if hasattr(req, "model_extra"):
|
485 |
+
req.model_extra.pop("stop", None)
|
486 |
+
else:
|
487 |
+
req.__dict__.pop("stop", None)
|
488 |
if not req.stream:
|
489 |
return call_api_sync(req)
|
490 |
else:
|
|
|
492 |
except Exception as e:
|
493 |
raise HTTPException(status_code=500, detail=str(e))
|
494 |
|
495 |
+
@app.post("/v1/embeddings", dependencies=[Depends(verify_api_key)])
|
496 |
+
async def embeddings(req: EmbeddingsRequest):
|
497 |
+
try:
|
498 |
+
client = get_openai_client()
|
499 |
+
payload = {k: v for k, v in req.model_dump().items() if v is not None}
|
500 |
+
print(f'------------------------------------------------------- INPUT ---------------------------------------------------------------\n{payload}')
|
501 |
+
response = client.embeddings.create(**payload)
|
502 |
+
print(f'------------------------------------------------------- OUTPUT ---------------------------------------------------------------\n{response}')
|
503 |
+
return response
|
504 |
+
except Exception as e:
|
505 |
+
raise HTTPException(status_code=500, detail=str(e))
|
506 |
+
|
507 |
@app.post("/v1/audio/transcriptions", dependencies=[Depends(verify_api_key)])
|
508 |
async def audio_transcriptions_endpoint(
|
509 |
file: UploadFile = File(...),
|