Upload 4 files
Browse files- Dockerfile +27 -0
- app.py +61 -0
- openapis.txt +3 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Item(BaseModel):
|
| 9 |
+
model: str
|
| 10 |
+
messages: list
|
| 11 |
+
temperature: float
|
| 12 |
+
max_tokens: Optional[int] = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 17 |
+
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 18 |
+
app = FastAPI()
|
| 19 |
+
|
| 20 |
+
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
| 21 |
+
|
| 22 |
+
def reog_file():
|
| 23 |
+
with open('./openapis.txt', 'r+') as file:
|
| 24 |
+
lines = file.readlines()
|
| 25 |
+
lines.append(lines[0])
|
| 26 |
+
lines = lines[1:]
|
| 27 |
+
file.seek(0)
|
| 28 |
+
file.writelines(lines)
|
| 29 |
+
file.close()
|
| 30 |
+
|
| 31 |
+
def read_file():
|
| 32 |
+
with open('./openapis.txt', 'r+') as file:
|
| 33 |
+
lines = file.readlines()
|
| 34 |
+
file.close()
|
| 35 |
+
return lines, len(lines)
|
| 36 |
+
|
| 37 |
+
def generate_chat_completion(data):
|
| 38 |
+
apis, num_apis = read_file()
|
| 39 |
+
api_index = 0
|
| 40 |
+
while(1):
|
| 41 |
+
api_key = apis[api_index][:-1]
|
| 42 |
+
print(api_key)
|
| 43 |
+
headers = {
|
| 44 |
+
"Content-Type": "application/json",
|
| 45 |
+
"Authorization": f"Bearer {api_key}",
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
response = requests.post(API_ENDPOINT, headers=headers, data=data.json())
|
| 49 |
+
if response.status_code == 200:
|
| 50 |
+
return response.json()
|
| 51 |
+
else:
|
| 52 |
+
api_index += 1
|
| 53 |
+
reog_file()
|
| 54 |
+
if api_index == num_apis:
|
| 55 |
+
raise Exception("Error")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@app.post("/generate")
|
| 59 |
+
def generate(data: Item):
|
| 60 |
+
output = generate_chat_completion(data)
|
| 61 |
+
return output
|
openapis.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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
|
| 2 |
+
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
|
| 3 |
+
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
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.74.*
|
| 2 |
+
requests==2.27.*
|
| 3 |
+
uvicorn[standard]==0.17.*
|
| 4 |
+
sentencepiece==0.1.*
|
| 5 |
+
pydantic
|