Spaces:
Sleeping
Sleeping
new-auth
Browse files
auth.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
|
3 |
+
def isAuthorized(authorization: Optional[str], TOKEN: str) -> bool:
|
4 |
+
if authorization is None:
|
5 |
+
return False
|
6 |
+
|
7 |
+
if not authorization.startswith("Token "):
|
8 |
+
return False
|
9 |
+
|
10 |
+
token = authorization.split(" ")[1]
|
11 |
+
|
12 |
+
return token == TOKEN
|
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from PIL import Image
|
@@ -10,6 +10,14 @@ from pydantic import BaseModel
|
|
10 |
from typing import Optional
|
11 |
from fastapi import FastAPI, HTTPException
|
12 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
app = FastAPI(
|
15 |
title="DOCUMANTICAI API",
|
@@ -20,11 +28,14 @@ app = FastAPI(
|
|
20 |
|
21 |
app.add_middleware(
|
22 |
CORSMiddleware,
|
23 |
-
allow_origins="*"
|
|
|
|
|
|
|
24 |
)
|
25 |
|
26 |
@app.post("/upload")
|
27 |
-
async def upload_image(fields:str, model:str, file: UploadFile = File(...)):
|
28 |
"""
|
29 |
### Endpoint Description:
|
30 |
Extract form data from an uploaded image and return the extracted data in JSON format.
|
@@ -39,6 +50,9 @@ async def upload_image(fields:str, model:str, file: UploadFile = File(...)):
|
|
39 |
"""
|
40 |
|
41 |
try:
|
|
|
|
|
|
|
42 |
# Load the uploaded image
|
43 |
image = Image.open(io.BytesIO(await file.read()))
|
44 |
|
@@ -121,7 +135,7 @@ class TranslateRequest(BaseModel):
|
|
121 |
target_language: str
|
122 |
|
123 |
@app.post("/translate")
|
124 |
-
async def translate_text_endpoint(request: TranslateRequest):
|
125 |
"""
|
126 |
### Endpoint Description:
|
127 |
Translate text to a specified language.
|
@@ -134,6 +148,9 @@ async def translate_text_endpoint(request: TranslateRequest):
|
|
134 |
- The translated text in the specified language.
|
135 |
"""
|
136 |
try:
|
|
|
|
|
|
|
137 |
response = translate_text(request.text, request.target_language)
|
138 |
response = json.loads(response)
|
139 |
return JSONResponse(content=response)
|
@@ -142,7 +159,7 @@ async def translate_text_endpoint(request: TranslateRequest):
|
|
142 |
|
143 |
|
144 |
# @app.get("/translate")
|
145 |
-
async def translate_text_endpoint(text: str, target_language: str):
|
146 |
"""
|
147 |
### Endpoint Description:
|
148 |
Translate text to a specified language.
|
@@ -156,6 +173,9 @@ async def translate_text_endpoint(text: str, target_language: str):
|
|
156 |
"""
|
157 |
|
158 |
try:
|
|
|
|
|
|
|
159 |
# print(target_language)
|
160 |
response = translate_text(text, target_language)
|
161 |
# print(response) #{"translated_text":"नमस्ते","translated_language":"नेपाली"}
|
@@ -170,11 +190,22 @@ class ChatRequest(BaseModel):
|
|
170 |
history: Optional[str] = None
|
171 |
|
172 |
@app.post("/chat")
|
173 |
-
def chat_endpoint(request: ChatRequest):
|
|
|
|
|
|
|
174 |
response_text = chat_text(request.text, request.query, request.history)
|
175 |
return {"reply": response_text}
|
176 |
|
177 |
# @app.get("/chat")
|
178 |
def chat_endpoint(query: str, text: str, history: Optional[str] = None):
|
179 |
response_text = chat_text(text, query, history)
|
180 |
-
return {"reply": response_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Header
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from PIL import Image
|
|
|
10 |
from typing import Optional
|
11 |
from fastapi import FastAPI, HTTPException
|
12 |
import json
|
13 |
+
import os
|
14 |
+
from auth import isAuthorized
|
15 |
+
|
16 |
+
PASSCODE = os.getenv("PASSCODE")
|
17 |
+
TOKEN = os.getenv("TOKEN")
|
18 |
+
|
19 |
+
if PASSCODE == None or TOKEN == None:
|
20 |
+
raise Exception("Please set the PASSCODE and TOKEN environment variables.")
|
21 |
|
22 |
app = FastAPI(
|
23 |
title="DOCUMANTICAI API",
|
|
|
28 |
|
29 |
app.add_middleware(
|
30 |
CORSMiddleware,
|
31 |
+
allow_origins=["*"],
|
32 |
+
allow_headers=["*"],
|
33 |
+
allow_credentials=True,
|
34 |
+
allow_methods=["*"],
|
35 |
)
|
36 |
|
37 |
@app.post("/upload")
|
38 |
+
async def upload_image(fields:str, model:str, file: UploadFile = File(...), authorization: Optional[str] = Header(None)):
|
39 |
"""
|
40 |
### Endpoint Description:
|
41 |
Extract form data from an uploaded image and return the extracted data in JSON format.
|
|
|
50 |
"""
|
51 |
|
52 |
try:
|
53 |
+
if not isAuthorized(authorization, TOKEN):
|
54 |
+
return JSONResponse(content="Unauthorized", status_code=401)
|
55 |
+
|
56 |
# Load the uploaded image
|
57 |
image = Image.open(io.BytesIO(await file.read()))
|
58 |
|
|
|
135 |
target_language: str
|
136 |
|
137 |
@app.post("/translate")
|
138 |
+
async def translate_text_endpoint(request: TranslateRequest, authorization: Optional[str] = Header(None)):
|
139 |
"""
|
140 |
### Endpoint Description:
|
141 |
Translate text to a specified language.
|
|
|
148 |
- The translated text in the specified language.
|
149 |
"""
|
150 |
try:
|
151 |
+
if not isAuthorized(authorization, TOKEN):
|
152 |
+
return JSONResponse(content="Unauthorized", status_code=401)
|
153 |
+
|
154 |
response = translate_text(request.text, request.target_language)
|
155 |
response = json.loads(response)
|
156 |
return JSONResponse(content=response)
|
|
|
159 |
|
160 |
|
161 |
# @app.get("/translate")
|
162 |
+
async def translate_text_endpoint(text: str, target_language: str, authorization: Optional[str] = Header(None)):
|
163 |
"""
|
164 |
### Endpoint Description:
|
165 |
Translate text to a specified language.
|
|
|
173 |
"""
|
174 |
|
175 |
try:
|
176 |
+
if not isAuthorized(authorization, TOKEN):
|
177 |
+
return JSONResponse(content="Unauthorized", status_code=401)
|
178 |
+
|
179 |
# print(target_language)
|
180 |
response = translate_text(text, target_language)
|
181 |
# print(response) #{"translated_text":"नमस्ते","translated_language":"नेपाली"}
|
|
|
190 |
history: Optional[str] = None
|
191 |
|
192 |
@app.post("/chat")
|
193 |
+
def chat_endpoint(request: ChatRequest, authorization: Optional[str] = Header(None)):
|
194 |
+
if not isAuthorized(authorization, TOKEN):
|
195 |
+
return JSONResponse(content="Unauthorized", status_code=401)
|
196 |
+
|
197 |
response_text = chat_text(request.text, request.query, request.history)
|
198 |
return {"reply": response_text}
|
199 |
|
200 |
# @app.get("/chat")
|
201 |
def chat_endpoint(query: str, text: str, history: Optional[str] = None):
|
202 |
response_text = chat_text(text, query, history)
|
203 |
+
return {"reply": response_text}
|
204 |
+
|
205 |
+
@app.get('/token')
|
206 |
+
def get_token(passcode: str):
|
207 |
+
if passcode == PASSCODE:
|
208 |
+
return {"token": TOKEN}
|
209 |
+
else:
|
210 |
+
return JSONResponse(content="invalid passcode", status_code=401)
|
211 |
+
|