Spaces:
Running
Running
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
12 |
+
'Meta-Llama-3.1-8B-Instruct': 'meta-llama/Meta-Llama-3.1-8B-Instruct',
|
13 |
+
'Meta-Llama-3.1-70B-Instruct': 'meta-llama/Meta-Llama-3.1-70B-Instruct',
|
14 |
+
'Meta-Llama-3.1-70B-Instruct-Turbo': 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo',
|
15 |
+
'Meta-Llama-3.1-405B-Instruct': 'meta-llama/Meta-Llama-3.1-405B-Instruct',
|
16 |
+
'Llama-3.2-11B-Vision-Instruct': 'meta-llama/Llama-3.2-11B-Vision-Instruct',
|
17 |
+
'Meta-Llama-3-8B-Instruct': 'meta-llama/Meta-Llama-3-8B-Instruct',
|
18 |
+
}
|
19 |
+
|
20 |
+
class Payload(BaseModel):
|
21 |
+
model: str
|
22 |
+
messages: list
|
23 |
+
|
24 |
+
@app.post("/api/v1/chat/completions")
|
25 |
+
async def get_completion(payload: Payload):
|
26 |
+
api_key = payload.headers.get("Authorization")
|
27 |
+
|
28 |
+
# API Key validation
|
29 |
+
if api_key not in valid_api_keys:
|
30 |
+
raise HTTPException(status_code=403, detail="Forbidden: Invalid API key")
|
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()
|
37 |
+
|
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 |
+
# Send the API request to the appropriate service
|
42 |
+
response = requests.post(url, json={**payload.dict(), "model": full_model_name})
|
43 |
+
|
44 |
+
if response.status_code != 200:
|
45 |
+
raise HTTPException(status_code=response.status_code, detail="Failed to fetch data")
|
46 |
+
|
47 |
+
# Return the response from the model API
|
48 |
+
return response.json()
|
49 |
+
|
50 |
+
# Run the server with Uvicorn using the 'main' module
|
51 |
+
if __name__ == "__main__":
|
52 |
+
import uvicorn
|
53 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|