subhrajit-mohanty
commited on
Commit
·
62de77b
1
Parent(s):
71e7552
initial commit
Browse files- Dockerfile +30 -0
- README.md +40 -1
- app.py +101 -0
- requirements.txt +4 -0
- utils.py +22 -0
Dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python 3.9 image
|
2 |
+
FROM python:3.9
|
3 |
+
|
4 |
+
# Set the working directory to /code
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
# Copy the current directory contents into the container at /code
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
12 |
+
|
13 |
+
# Set up a new user named "user" with user ID 1000
|
14 |
+
RUN useradd -m -u 1000 user
|
15 |
+
# Switch to the "user" user
|
16 |
+
USER user
|
17 |
+
# Set home to the user's home directory
|
18 |
+
ENV HOME=/home/user \
|
19 |
+
PATH=/home/user/.local/bin:$PATH
|
20 |
+
|
21 |
+
# Set the working directory to the user's home directory
|
22 |
+
WORKDIR $HOME/app
|
23 |
+
|
24 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
25 |
+
COPY --chown=user . $HOME/app
|
26 |
+
|
27 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
28 |
+
# CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
29 |
+
|
30 |
+
CMD gunicorn -k uvicorn.workers.UvicornWorker --workers 2 --threads=2 --max-requests 512 --bind 0.0.0.0:7860 app:app
|
README.md
CHANGED
@@ -8,4 +8,43 @@ pinned: false
|
|
8 |
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
+
## How to Use the API using python
|
12 |
+
|
13 |
+
### Create a Token
|
14 |
+
```
|
15 |
+
import requests
|
16 |
+
|
17 |
+
headers = {
|
18 |
+
'accept': 'application/json',
|
19 |
+
'Content-Type': 'application/json',
|
20 |
+
}
|
21 |
+
|
22 |
+
json_data = {
|
23 |
+
"ref_key": "F0eeQ419wvoCSPH7KBmsd9OiVkF0W0DxK1XE9T3BlbkFJ0",
|
24 |
+
"expiry_date": "2023-06-15"
|
25 |
+
}
|
26 |
+
|
27 |
+
response = requests.post('http://0.0.0.0:8000/create', headers=headers, json=json_data)
|
28 |
+
token = response.json()
|
29 |
+
```
|
30 |
+
### calling open API (text-davinci-002/gpt-3.5-turbo)
|
31 |
+
```
|
32 |
+
import requests
|
33 |
+
|
34 |
+
headers = {
|
35 |
+
'accept': 'application/json',
|
36 |
+
'Content-Type': 'application/json',
|
37 |
+
'Authorization': 'Bearer TOKEN',
|
38 |
+
}
|
39 |
+
|
40 |
+
json_data = {
|
41 |
+
'message': 'write a joke for me',
|
42 |
+
"openAI_token": OPENAI_KEY,
|
43 |
+
"model_name" : "gpt-3.5-turbo"
|
44 |
+
}
|
45 |
+
|
46 |
+
response = requests.post('http://0.0.0.0:8000/chat', headers=headers, json=json_data)
|
47 |
+
if response.status_code == 200:
|
48 |
+
print(response.json())
|
49 |
+
```
|
50 |
+
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from fastapi import FastAPI, HTTPException, Depends, Request, Response
|
5 |
+
from fastapi.security import OAuth2PasswordBearer
|
6 |
+
from tinydb import TinyDB
|
7 |
+
from tinydb import Query
|
8 |
+
from datetime import datetime
|
9 |
+
from utils import generate_token
|
10 |
+
|
11 |
+
query = Query()
|
12 |
+
db = TinyDB(".token.json")
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
17 |
+
|
18 |
+
REF_KEY = "F0eeQ419wvoCSPH7KBmsd9OiVkF0W0DxK1XE9T3BlbkFJ0"
|
19 |
+
model_name = ""
|
20 |
+
|
21 |
+
def verify_token(token: str = Depends(oauth2_scheme)):
|
22 |
+
expiry = -1
|
23 |
+
res = db.get(query.token == token)
|
24 |
+
|
25 |
+
if res:
|
26 |
+
expiry = (datetime.strptime(res["expiry_date"], '%Y-%m-%d') - datetime.strptime(res["created_at"], '%Y-%m-%d')).days
|
27 |
+
|
28 |
+
if expiry < 0:
|
29 |
+
return {"message": "Token is not Valid"}
|
30 |
+
|
31 |
+
return token
|
32 |
+
|
33 |
+
|
34 |
+
class ChatInput(BaseModel):
|
35 |
+
message: str
|
36 |
+
openAI_token: str
|
37 |
+
model_name: str
|
38 |
+
|
39 |
+
|
40 |
+
class RefToken(BaseModel):
|
41 |
+
expiry_date: str
|
42 |
+
ref_key: str
|
43 |
+
|
44 |
+
|
45 |
+
@app.post("/create")
|
46 |
+
async def create(data: RefToken):
|
47 |
+
token = "Reference Key is incorrect"
|
48 |
+
try:
|
49 |
+
if data.ref_key == REF_KEY:
|
50 |
+
token = generate_token(data.expiry_date, db)
|
51 |
+
return {"TOKEN": token}
|
52 |
+
except Exception as e:
|
53 |
+
raise HTTPException(status_code=500, detail="An error occurred while processing the request." + str(e))
|
54 |
+
|
55 |
+
@app.get("/list")
|
56 |
+
async def create(token: str = Depends(verify_token)):
|
57 |
+
token = "Reference Key is incorrect"
|
58 |
+
try:
|
59 |
+
data = db.all()
|
60 |
+
return {"data": data}
|
61 |
+
except Exception as e:
|
62 |
+
raise HTTPException(status_code=500, detail="An error occurred while processing the request." + str(e))
|
63 |
+
|
64 |
+
@app.post("/chat")
|
65 |
+
async def chat_with_gpt(chat_input: ChatInput, token: str = Depends(verify_token)):
|
66 |
+
openai.api_key = chat_input.openAI_token
|
67 |
+
prompt = f"User: {chat_input.message}\nAI:"
|
68 |
+
model_name = chat_input.model_name
|
69 |
+
try:
|
70 |
+
if model_name == "text-davinci-002":
|
71 |
+
response = openai.Completion.create(
|
72 |
+
engine="text-davinci-002",
|
73 |
+
prompt=prompt,
|
74 |
+
max_tokens=50,
|
75 |
+
n=1,
|
76 |
+
stop=None,
|
77 |
+
temperature=0.7,
|
78 |
+
)
|
79 |
+
message = response.choices[0].text.strip()
|
80 |
+
usages = response["usage"]
|
81 |
+
|
82 |
+
if model_name == "gpt-3.5-turbo":
|
83 |
+
response = openai.ChatCompletion.create(
|
84 |
+
model="gpt-3.5-turbo",
|
85 |
+
messages=[
|
86 |
+
{"role": "system", "content": prompt}
|
87 |
+
])
|
88 |
+
|
89 |
+
message = response.choices[0]["message"]["content"]
|
90 |
+
usages = response["usage"]
|
91 |
+
|
92 |
+
if model_name == "":
|
93 |
+
message = "Plase select the model"
|
94 |
+
usages = ""
|
95 |
+
|
96 |
+
return {
|
97 |
+
"message": message,
|
98 |
+
"usages" : usages
|
99 |
+
}
|
100 |
+
except Exception as e:
|
101 |
+
raise HTTPException(status_code=500, detail="An error occurred while processing the request." + str(e))
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.95.1
|
2 |
+
uvicorn==0.22.0
|
3 |
+
openai==0.27.6
|
4 |
+
tinydb==4.7.1
|
utils.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime, timedelta, date
|
2 |
+
import secrets
|
3 |
+
import random
|
4 |
+
import string
|
5 |
+
|
6 |
+
|
7 |
+
def generate_token(expiry_date, db):
|
8 |
+
alphabet = string.ascii_letters # Get a string containing all the alphabets (both upper and lower case)
|
9 |
+
first_char = random.choice(alphabet) # Randomly choose an alphabet as the first character
|
10 |
+
token = "md-" + first_char + secrets.token_hex(60)
|
11 |
+
|
12 |
+
creation_date = date.today().strftime("%Y-%m-%d")
|
13 |
+
|
14 |
+
token_details = {
|
15 |
+
"created_at": creation_date,
|
16 |
+
"expiry_date" : expiry_date,
|
17 |
+
"token" : token
|
18 |
+
}
|
19 |
+
|
20 |
+
db.insert(token_details)
|
21 |
+
|
22 |
+
return token
|