Sneha Dixit commited on
Commit
82276a6
·
0 Parent(s):

[ADDED] api with api key auth

Browse files
Files changed (7) hide show
  1. .gitignore +2 -0
  2. Dockerfile +9 -0
  3. auth.py +9 -0
  4. client.py +23 -0
  5. constants.py +3 -0
  6. requirements.txt +3 -0
  7. server.py +13 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11.0
2
+
3
+ COPY . .
4
+
5
+ WORKDIR /
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /requirements.txt
8
+
9
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
auth.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi.security.api_key import APIKeyHeader
2
+ from fastapi import Security, HTTPException
3
+
4
+ API_Keys = ["abc"]
5
+ api_key_header = APIKeyHeader(name="x-api-key", auto_error=False)
6
+
7
+ async def api_key_auth(api_key: str = Security(api_key_header)):
8
+ if api_key not in API_Keys:
9
+ raise HTTPException(status_code=401, detail="Missing or invalid API key")
client.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import AzureOpenAI
2
+ import constants
3
+
4
+ client = AzureOpenAI(
5
+ api_key=constants.AZURE_OPENAI_API_KEY,
6
+ api_version=constants.API_VERSION,
7
+ azure_endpoint = constants.AZURE_OPENAI_ENDPOINT
8
+ )
9
+
10
+ prompt = 'Write a tagline for an ice cream shop.'
11
+
12
+ response = client.completions.create(
13
+ model='gpt-35-turbo-instruct',
14
+ prompt=prompt,
15
+ max_tokens=50,
16
+ n=1,
17
+ stop=None,
18
+ temperature=0.7
19
+ )
20
+
21
+ result = response.choices[0].text.strip()
22
+
23
+ print(result)
constants.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ AZURE_OPENAI_API_KEY=''
2
+ AZURE_OPENAI_ENDPOINT=''
3
+ API_VERSION='2024-02-01'
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi==0.99.1
2
+ uvicorn
3
+ openai
server.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Depends
2
+ import auth
3
+
4
+ app = FastAPI()
5
+
6
+ @app.get("/")
7
+ async def welcome():
8
+ return "Hello, Welcome to AI space!"
9
+
10
+ @app.get("/generate")
11
+ async def generate(api_key = Depends(auth.api_key_auth)):
12
+ # add the logic to call gpt here
13
+ return "All good. You only get this message if you're authenticated"