🐛 Bug: Fix the bug where the client uploads the image URL instead of converting it to base64 encoding during image recognition.
Browse files- main.py +1 -0
- request.py +43 -1
main.py
CHANGED
|
@@ -492,6 +492,7 @@ def verify_admin_api_key(credentials: HTTPAuthorizationCredentials = Depends(sec
|
|
| 492 |
|
| 493 |
@app.post("/v1/chat/completions", dependencies=[Depends(rate_limit_dependency)])
|
| 494 |
async def request_model(request: Union[RequestModel, ImageGenerationRequest], token: str = Depends(verify_api_key)):
|
|
|
|
| 495 |
return await model_handler.request_model(request, token)
|
| 496 |
|
| 497 |
@app.options("/v1/chat/completions", dependencies=[Depends(rate_limit_dependency)])
|
|
|
|
| 492 |
|
| 493 |
@app.post("/v1/chat/completions", dependencies=[Depends(rate_limit_dependency)])
|
| 494 |
async def request_model(request: Union[RequestModel, ImageGenerationRequest], token: str = Depends(verify_api_key)):
|
| 495 |
+
# logger.info(f"Request received: {request}")
|
| 496 |
return await model_handler.request_model(request, token)
|
| 497 |
|
| 498 |
@app.options("/v1/chat/completions", dependencies=[Depends(rate_limit_dependency)])
|
request.py
CHANGED
|
@@ -1,12 +1,54 @@
|
|
|
|
|
| 1 |
import json
|
| 2 |
from models import RequestModel
|
| 3 |
from utils import c35s, c3s, c3o, c3h, gem, BaseAPI
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
async def get_image_message(base64_image, engine = None):
|
|
|
|
|
|
|
| 6 |
colon_index = base64_image.index(":")
|
| 7 |
semicolon_index = base64_image.index(";")
|
| 8 |
image_type = base64_image[colon_index + 1:semicolon_index]
|
| 9 |
-
|
| 10 |
if "gpt" == engine:
|
| 11 |
return {
|
| 12 |
"type": "image_url",
|
|
|
|
| 1 |
+
import os
|
| 2 |
import json
|
| 3 |
from models import RequestModel
|
| 4 |
from utils import c35s, c3s, c3o, c3h, gem, BaseAPI
|
| 5 |
|
| 6 |
+
import base64
|
| 7 |
+
import urllib.parse
|
| 8 |
+
|
| 9 |
+
def encode_image(image_path):
|
| 10 |
+
with open(image_path, "rb") as image_file:
|
| 11 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 12 |
+
|
| 13 |
+
async def get_doc_from_url(url):
|
| 14 |
+
filename = urllib.parse.unquote(url.split("/")[-1])
|
| 15 |
+
transport = httpx.AsyncHTTPTransport(
|
| 16 |
+
http2=True,
|
| 17 |
+
verify=False,
|
| 18 |
+
retries=1
|
| 19 |
+
)
|
| 20 |
+
async with httpx.AsyncClient(transport=transport) as client:
|
| 21 |
+
try:
|
| 22 |
+
response = await client.get(
|
| 23 |
+
url,
|
| 24 |
+
timeout=30.0
|
| 25 |
+
)
|
| 26 |
+
with open(filename, 'wb') as f:
|
| 27 |
+
f.write(response.content)
|
| 28 |
+
|
| 29 |
+
except httpx.RequestError as e:
|
| 30 |
+
print(f"An error occurred while requesting {e.request.url!r}.")
|
| 31 |
+
|
| 32 |
+
return filename
|
| 33 |
+
|
| 34 |
+
async def get_encode_image(image_url):
|
| 35 |
+
filename = await get_doc_from_url(image_url)
|
| 36 |
+
image_path = os.getcwd() + "/" + filename
|
| 37 |
+
base64_image = encode_image(image_path)
|
| 38 |
+
if filename.endswith(".png"):
|
| 39 |
+
prompt = f"data:image/png;base64,{base64_image}"
|
| 40 |
+
else:
|
| 41 |
+
prompt = f"data:image/jpeg;base64,{base64_image}"
|
| 42 |
+
os.remove(image_path)
|
| 43 |
+
return prompt
|
| 44 |
+
|
| 45 |
async def get_image_message(base64_image, engine = None):
|
| 46 |
+
if base64_image.startswith("http"):
|
| 47 |
+
base64_image = await get_encode_image(base64_image)
|
| 48 |
colon_index = base64_image.index(":")
|
| 49 |
semicolon_index = base64_image.index(";")
|
| 50 |
image_type = base64_image[colon_index + 1:semicolon_index]
|
| 51 |
+
|
| 52 |
if "gpt" == engine:
|
| 53 |
return {
|
| 54 |
"type": "image_url",
|