Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
import requests
|
4 |
import json
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
# Model and API Key validation
|
9 |
valid_api_keys = ['PARTH-SADARIA-NI-API-CHAWI', 'HEET-NI-CHEESEWADI-API-KEY']
|
10 |
model_aliases = {
|
11 |
'Llama-3.1-Nemotron-70B-Instruct': 'nvidia/Llama-3.1-Nemotron-70B-Instruct',
|
@@ -22,8 +21,8 @@ class Payload(BaseModel):
|
|
22 |
messages: list
|
23 |
|
24 |
@app.post("/api/v1/chat/completions")
|
25 |
-
async def get_completion(payload: Payload):
|
26 |
-
api_key =
|
27 |
|
28 |
# API Key validation
|
29 |
if api_key not in valid_api_keys:
|
@@ -31,6 +30,9 @@ async def get_completion(payload: Payload):
|
|
31 |
|
32 |
# Model alias resolution
|
33 |
user_model = payload.model
|
|
|
|
|
|
|
34 |
full_model_name = model_aliases.get(user_model, user_model)
|
35 |
|
36 |
is_deepinfra_model = full_model_name in model_aliases.values()
|
@@ -38,14 +40,16 @@ async def get_completion(payload: Payload):
|
|
38 |
# Determine the URL to send the request to
|
39 |
url = "https://api.deepinfra.com/v1/openai/chat/completions" if is_deepinfra_model else "https://gpt.tiptopuni.com/api/openai/v1/chat/completions"
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
raise HTTPException(status_code=
|
46 |
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
# Run the server with Uvicorn using the 'main' module
|
51 |
if __name__ == "__main__":
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Request
|
2 |
from pydantic import BaseModel
|
3 |
import requests
|
4 |
import json
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
|
|
8 |
valid_api_keys = ['PARTH-SADARIA-NI-API-CHAWI', 'HEET-NI-CHEESEWADI-API-KEY']
|
9 |
model_aliases = {
|
10 |
'Llama-3.1-Nemotron-70B-Instruct': 'nvidia/Llama-3.1-Nemotron-70B-Instruct',
|
|
|
21 |
messages: list
|
22 |
|
23 |
@app.post("/api/v1/chat/completions")
|
24 |
+
async def get_completion(payload: Payload, request: Request):
|
25 |
+
api_key = request.headers.get("Authorization")
|
26 |
|
27 |
# API Key validation
|
28 |
if api_key not in valid_api_keys:
|
|
|
30 |
|
31 |
# Model alias resolution
|
32 |
user_model = payload.model
|
33 |
+
if user_model not in model_aliases:
|
34 |
+
raise HTTPException(status_code=400, detail="Invalid model name")
|
35 |
+
|
36 |
full_model_name = model_aliases.get(user_model, user_model)
|
37 |
|
38 |
is_deepinfra_model = full_model_name in model_aliases.values()
|
|
|
40 |
# Determine the URL to send the request to
|
41 |
url = "https://api.deepinfra.com/v1/openai/chat/completions" if is_deepinfra_model else "https://gpt.tiptopuni.com/api/openai/v1/chat/completions"
|
42 |
|
43 |
+
try:
|
44 |
+
response = requests.post(url, json={**payload.dict(), "model": full_model_name})
|
45 |
+
response.raise_for_status() # Raises HTTPError if the response status code is 4xx/5xx
|
46 |
+
except requests.exceptions.RequestException as e:
|
47 |
+
raise HTTPException(status_code=500, detail=f"Request failed: {e}")
|
48 |
|
49 |
+
try:
|
50 |
+
return response.json()
|
51 |
+
except ValueError:
|
52 |
+
raise HTTPException(status_code=500, detail="Non-JSON response received")
|
53 |
|
54 |
# Run the server with Uvicorn using the 'main' module
|
55 |
if __name__ == "__main__":
|