ParthSadaria commited on
Commit
4986fe4
·
verified ·
1 Parent(s): f0af877

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -10
main.py CHANGED
@@ -1,17 +1,27 @@
 
 
1
  from fastapi import FastAPI, HTTPException, Request
2
  from fastapi.responses import StreamingResponse, HTMLResponse
3
  from pydantic import BaseModel
4
  import requests
5
- import time
6
  from functools import lru_cache
7
 
 
 
 
8
  app = FastAPI()
9
 
10
  # Create a session for reusing the HTTP connection
11
  session = requests.Session()
12
 
13
- # Valid API keys
14
- valid_api_keys = ['PARTH-SADARIA-NI-API-CHAWI', 'HEET-NI-CHEESEWADI-API-KEY']
 
 
 
 
 
 
15
 
16
  class Payload(BaseModel):
17
  model: str
@@ -27,9 +37,9 @@ async def root():
27
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
28
  <title>Loki.AI API</title>
29
  <style>
30
- body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
31
  h1 { color: #4CAF50; }
32
- a { color: #007BFF; text-decoration: none; }
33
  a:hover { text-decoration: underline; }
34
  </style>
35
  </head>
@@ -47,7 +57,7 @@ async def root():
47
  @lru_cache(maxsize=1)
48
  def get_cached_models():
49
  try:
50
- response = session.get("https://lokiai.vercel.app/api/v1/models", timeout=3)
51
  response.raise_for_status()
52
  return response.json()
53
  except requests.exceptions.RequestException as e:
@@ -65,16 +75,13 @@ async def get_completion(payload: Payload, request: Request):
65
  if api_key not in valid_api_keys:
66
  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 :)")
67
 
68
- # Set the URL for all requests
69
- url = "https://gpt.tiptopuni.com/api/openai/v1/chat/completions"
70
-
71
  # Prepare the payload for streaming
72
  payload_dict = {**payload.dict(), "stream": True}
73
 
74
  # Define a generator to stream the response
75
  def stream_generator():
76
  try:
77
- with session.post(url, json=payload_dict, stream=True, timeout=15) as response:
78
  response.raise_for_status()
79
  for chunk in response.iter_content(chunk_size=1024):
80
  if chunk: # Only yield non-empty chunks
 
1
+ import os
2
+ from dotenv import load_dotenv
3
  from fastapi import FastAPI, HTTPException, Request
4
  from fastapi.responses import StreamingResponse, HTMLResponse
5
  from pydantic import BaseModel
6
  import requests
 
7
  from functools import lru_cache
8
 
9
+ # Load environment variables from .env file
10
+ load_dotenv()
11
+
12
  app = FastAPI()
13
 
14
  # Create a session for reusing the HTTP connection
15
  session = requests.Session()
16
 
17
+ # Get API keys and secret endpoint from environment variables
18
+ api_keys_str = os.getenv('API_KEYS')
19
+ valid_api_keys = api_keys_str.split(',') if api_keys_str else []
20
+ secret_api_endpoint = os.getenv('SECRET_API_ENDPOINT')
21
+
22
+ # Check if the endpoint is set in the environment
23
+ if not secret_api_endpoint:
24
+ raise HTTPException(status_code=500, detail="Secret API endpoint is not configured in environment variables.")
25
 
26
  class Payload(BaseModel):
27
  model: str
 
37
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
38
  <title>Loki.AI API</title>
39
  <style>
40
+ body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; background-color: #121212; color: white; }
41
  h1 { color: #4CAF50; }
42
+ a { color: #BB86FC; text-decoration: none; }
43
  a:hover { text-decoration: underline; }
44
  </style>
45
  </head>
 
57
  @lru_cache(maxsize=1)
58
  def get_cached_models():
59
  try:
60
+ response = session.get(f"{secret_api_endpoint}/api/v1/models", timeout=3)
61
  response.raise_for_status()
62
  return response.json()
63
  except requests.exceptions.RequestException as e:
 
75
  if api_key not in valid_api_keys:
76
  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 :)")
77
 
 
 
 
78
  # Prepare the payload for streaming
79
  payload_dict = {**payload.dict(), "stream": True}
80
 
81
  # Define a generator to stream the response
82
  def stream_generator():
83
  try:
84
+ with session.post(secret_api_endpoint, json=payload_dict, stream=True, timeout=15) as response:
85
  response.raise_for_status()
86
  for chunk in response.iter_content(chunk_size=1024):
87
  if chunk: # Only yield non-empty chunks