ParthSadaria commited on
Commit
6a84e5c
·
verified ·
1 Parent(s): 7073dea

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -11
main.py CHANGED
@@ -10,7 +10,7 @@ app = FastAPI()
10
  valid_api_keys = ['PARTH-SADARIA-NI-API-CHAWI', 'HEET-NI-CHEESEWADI-API-KEY']
11
 
12
  # Fetch the secret API endpoint from environment variables
13
- SECRET_API_ENDPOINT = os.getenv("SECRET_API_ENDPOINT", "Default_Endpoint_Not_Set")
14
 
15
  class Payload(BaseModel):
16
  model: str
@@ -29,11 +29,6 @@ async def get_models():
29
  except requests.exceptions.RequestException as e:
30
  raise HTTPException(status_code=500, detail=f"Request failed: {e}")
31
 
32
- @app.get("/api-endpoint")
33
- async def get_secret_api_endpoint():
34
- # Return the secret API endpoint
35
- return {"secret_api_endpoint": SECRET_API_ENDPOINT}
36
-
37
  @app.post("/v1/chat/completions")
38
  async def get_completion(payload: Payload, request: Request):
39
  api_key = request.headers.get("Authorization")
@@ -42,16 +37,13 @@ async def get_completion(payload: Payload, request: Request):
42
  if api_key not in valid_api_keys:
43
  raise HTTPException(status_code=403, detail="Forbidden: Invalid API key. Join dsc.gg/chadgang and DM @mr_leaderyt on Discord for a free API key :)")
44
 
45
- # Set the URL for all requests
46
- url = "https://gpt.tiptopuni.com/api/openai/v1/chat/completions"
47
-
48
  # Prepare the payload for streaming
49
  payload_dict = {**payload.dict(), "stream": True}
50
 
51
  # Define a generator to stream the response
52
  def stream_generator():
53
  try:
54
- with requests.post(url, json=payload_dict, stream=True, timeout=5) as response:
55
  response.raise_for_status()
56
  for chunk in response.iter_content(chunk_size=1024):
57
  if chunk: # Only yield non-empty chunks
@@ -59,6 +51,16 @@ async def get_completion(payload: Payload, request: Request):
59
  except requests.exceptions.RequestException as e:
60
  raise HTTPException(status_code=500, detail=f"Streaming failed: {e}")
61
 
 
 
 
 
 
 
 
 
 
 
62
  # Return the streaming response
63
  return StreamingResponse(stream_generator(), media_type="application/json")
64
 
@@ -69,4 +71,8 @@ async def startup_event():
69
  print("GET /")
70
  print("GET /models")
71
  print("POST /v1/chat/completions")
72
- print
 
 
 
 
 
10
  valid_api_keys = ['PARTH-SADARIA-NI-API-CHAWI', 'HEET-NI-CHEESEWADI-API-KEY']
11
 
12
  # Fetch the secret API endpoint from environment variables
13
+ api_endpoint = os.getenv("SECRET_API_ENDPOINT")
14
 
15
  class Payload(BaseModel):
16
  model: str
 
29
  except requests.exceptions.RequestException as e:
30
  raise HTTPException(status_code=500, detail=f"Request failed: {e}")
31
 
 
 
 
 
 
32
  @app.post("/v1/chat/completions")
33
  async def get_completion(payload: Payload, request: Request):
34
  api_key = request.headers.get("Authorization")
 
37
  if api_key not in valid_api_keys:
38
  raise HTTPException(status_code=403, detail="Forbidden: Invalid API key. Join dsc.gg/chadgang and DM @mr_leaderyt on Discord for a free API key :)")
39
 
 
 
 
40
  # Prepare the payload for streaming
41
  payload_dict = {**payload.dict(), "stream": True}
42
 
43
  # Define a generator to stream the response
44
  def stream_generator():
45
  try:
46
+ with requests.post(api_endpoint, json=payload_dict, stream=True, timeout=10) as response:
47
  response.raise_for_status()
48
  for chunk in response.iter_content(chunk_size=1024):
49
  if chunk: # Only yield non-empty chunks
 
51
  except requests.exceptions.RequestException as e:
52
  raise HTTPException(status_code=500, detail=f"Streaming failed: {e}")
53
 
54
+ # Send a POST request to the secret API endpoint
55
+ try:
56
+ secret_api_response = requests.post(api_endpoint, json=payload_dict, timeout=5)
57
+ secret_api_response.raise_for_status() # Check if the request was successful
58
+ # Log or process the secret API response if needed
59
+ print(secret_api_response.json()) # Just an example of processing the response
60
+
61
+ except requests.exceptions.RequestException as e:
62
+ raise HTTPException(status_code=500, detail=f"Failed to POST to secret API endpoint: {e}")
63
+
64
  # Return the streaming response
65
  return StreamingResponse(stream_generator(), media_type="application/json")
66
 
 
71
  print("GET /")
72
  print("GET /models")
73
  print("POST /v1/chat/completions")
74
+
75
+ # Run the server with Uvicorn using the 'main' module
76
+ if __name__ == "__main__":
77
+ import uvicorn
78
+ uvicorn.run(app, host="0.0.0.0", port=8000)