Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -32,20 +32,6 @@ if not secret_api_endpoint or not secret_api_endpoint_2 or not secret_api_endpoi
|
|
| 32 |
alternate_models = {"gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"}
|
| 33 |
|
| 34 |
available_model_ids = []
|
| 35 |
-
def check_server_status():
|
| 36 |
-
server_down = True
|
| 37 |
-
|
| 38 |
-
def decorator(func):
|
| 39 |
-
async def wrapper(*args, **kwargs):
|
| 40 |
-
if server_down:
|
| 41 |
-
raise HTTPException(
|
| 42 |
-
status_code=503,
|
| 43 |
-
detail="Server is currently unavailable. All services are temporarily down."
|
| 44 |
-
)
|
| 45 |
-
return await func(*args, **kwargs)
|
| 46 |
-
return wrapper
|
| 47 |
-
return decorator
|
| 48 |
-
|
| 49 |
class Payload(BaseModel):
|
| 50 |
model: str
|
| 51 |
messages: list
|
|
@@ -157,30 +143,41 @@ async def fetch_models():
|
|
| 157 |
return await get_models()
|
| 158 |
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
@app.post("/chat/completions")
|
| 161 |
@app.post("/v1/chat/completions")
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
model_to_use = payload.model if payload.model else "gpt-4o-mini"
|
|
|
|
| 165 |
# Validate model availability
|
| 166 |
if model_to_use not in available_model_ids:
|
| 167 |
raise HTTPException(
|
| 168 |
-
status_code=400,
|
| 169 |
detail=f"Model '{model_to_use}' is not available. Check /models for the available model list."
|
| 170 |
)
|
| 171 |
-
|
| 172 |
# Proceed with the request handling
|
| 173 |
payload_dict = payload.dict()
|
| 174 |
payload_dict["model"] = model_to_use
|
| 175 |
-
|
| 176 |
# Select the appropriate endpoint
|
| 177 |
endpoint = secret_api_endpoint_2 if model_to_use in alternate_models else secret_api_endpoint
|
| 178 |
current_time = (datetime.datetime.utcnow() + datetime.timedelta(hours=5, minutes=30)).strftime("%Y-%m-%d %I:%M:%S %p")
|
| 179 |
aaip = request.client.host
|
| 180 |
print(f"Time: {current_time}, {aaip}")
|
| 181 |
print(payload_dict)
|
| 182 |
-
|
| 183 |
-
|
| 184 |
async def stream_generator(payload_dict):
|
| 185 |
scraper = cloudscraper.create_scraper() # Create a CloudScraper session
|
| 186 |
try:
|
|
@@ -198,12 +195,11 @@ async def get_completion(payload: Payload,request: Request):
|
|
| 198 |
raise HTTPException(status_code=404, detail="The requested resource was not found.")
|
| 199 |
elif response.status_code >= 500:
|
| 200 |
raise HTTPException(status_code=500, detail="Server error. Try again later.")
|
| 201 |
-
|
| 202 |
# Stream response lines to the client
|
| 203 |
for line in response.iter_lines():
|
| 204 |
if line:
|
| 205 |
yield line.decode('utf-8') + "\n"
|
| 206 |
-
|
| 207 |
except requests.exceptions.RequestException as req_err:
|
| 208 |
# Handle request-specific errors
|
| 209 |
print(response.text)
|
|
@@ -212,7 +208,7 @@ async def get_completion(payload: Payload,request: Request):
|
|
| 212 |
# Handle unexpected errors
|
| 213 |
print(response.text)
|
| 214 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
| 215 |
-
|
| 216 |
return StreamingResponse(stream_generator(payload_dict), media_type="application/json")
|
| 217 |
# Remove the duplicated endpoint and combine the functionality
|
| 218 |
@app.get("/images/generations") #pollinations.ai thanks to them :)
|
|
|
|
| 32 |
alternate_models = {"gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"}
|
| 33 |
|
| 34 |
available_model_ids = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
class Payload(BaseModel):
|
| 36 |
model: str
|
| 37 |
messages: list
|
|
|
|
| 143 |
return await get_models()
|
| 144 |
|
| 145 |
|
| 146 |
+
from fastapi import HTTPException
|
| 147 |
+
from fastapi.responses import JSONResponse
|
| 148 |
+
|
| 149 |
+
server_status = False # Set to False to simulate the server being down
|
| 150 |
+
|
| 151 |
@app.post("/chat/completions")
|
| 152 |
@app.post("/v1/chat/completions")
|
| 153 |
+
async def get_completion(payload: Payload, request: Request):
|
| 154 |
+
# Check server status
|
| 155 |
+
if not server_status:
|
| 156 |
+
return JSONResponse(
|
| 157 |
+
status_code=503,
|
| 158 |
+
content={"message": "Server is down. Please try again later."}
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
model_to_use = payload.model if payload.model else "gpt-4o-mini"
|
| 162 |
+
|
| 163 |
# Validate model availability
|
| 164 |
if model_to_use not in available_model_ids:
|
| 165 |
raise HTTPException(
|
| 166 |
+
status_code=400,
|
| 167 |
detail=f"Model '{model_to_use}' is not available. Check /models for the available model list."
|
| 168 |
)
|
| 169 |
+
|
| 170 |
# Proceed with the request handling
|
| 171 |
payload_dict = payload.dict()
|
| 172 |
payload_dict["model"] = model_to_use
|
| 173 |
+
|
| 174 |
# Select the appropriate endpoint
|
| 175 |
endpoint = secret_api_endpoint_2 if model_to_use in alternate_models else secret_api_endpoint
|
| 176 |
current_time = (datetime.datetime.utcnow() + datetime.timedelta(hours=5, minutes=30)).strftime("%Y-%m-%d %I:%M:%S %p")
|
| 177 |
aaip = request.client.host
|
| 178 |
print(f"Time: {current_time}, {aaip}")
|
| 179 |
print(payload_dict)
|
| 180 |
+
|
|
|
|
| 181 |
async def stream_generator(payload_dict):
|
| 182 |
scraper = cloudscraper.create_scraper() # Create a CloudScraper session
|
| 183 |
try:
|
|
|
|
| 195 |
raise HTTPException(status_code=404, detail="The requested resource was not found.")
|
| 196 |
elif response.status_code >= 500:
|
| 197 |
raise HTTPException(status_code=500, detail="Server error. Try again later.")
|
| 198 |
+
|
| 199 |
# Stream response lines to the client
|
| 200 |
for line in response.iter_lines():
|
| 201 |
if line:
|
| 202 |
yield line.decode('utf-8') + "\n"
|
|
|
|
| 203 |
except requests.exceptions.RequestException as req_err:
|
| 204 |
# Handle request-specific errors
|
| 205 |
print(response.text)
|
|
|
|
| 208 |
# Handle unexpected errors
|
| 209 |
print(response.text)
|
| 210 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
| 211 |
+
|
| 212 |
return StreamingResponse(stream_generator(payload_dict), media_type="application/json")
|
| 213 |
# Remove the duplicated endpoint and combine the functionality
|
| 214 |
@app.get("/images/generations") #pollinations.ai thanks to them :)
|