File size: 7,964 Bytes
b71e69d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from asyncio import gather
from base64 import b64decode
from binascii import Error as BinasciiError
from contextlib import asynccontextmanager
from io import BytesIO
from logging import Formatter, INFO, StreamHandler, getLogger
from pathlib import Path
from random import randint
from typing import AsyncGenerator
from uuid import UUID

from PIL.Image import open as image_open
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse
from httpx import AsyncClient
from starlette.responses import Response

logger = getLogger('NVIDIA_VLM_API')
logger.setLevel(INFO)
handler = StreamHandler()
handler.setLevel(INFO)
formatter = Formatter('%(asctime)s | %(levelname)s : %(message)s', datefmt='%d.%m.%Y %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info('инициализация приложения...')

INVOKE_URLS = [
    'https://ai.api.nvidia.com/v1/vlm/microsoft/phi-3-vision-128k-instruct',
    'https://ai.api.nvidia.com/v1/vlm/nvidia/neva-22b',
    'https://ai.api.nvidia.com/v1/vlm/nvidia/vila'
]

ASSETS_URL = 'https://api.nvcf.nvidia.com/v2/nvcf/assets'


def get_extension(filename: str) -> str:
    return Path(filename).suffix[1:].lower()


async def upload_asset(client: AsyncClient, media_file_bytes: bytes, description: str, api_key: str) -> UUID:
    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json',
        'accept': 'application/json',
    }

    authorize_response = await client.post(
        ASSETS_URL,
        headers=headers,
        json={'contentType': 'image/jpeg', 'description': description},
        timeout=30,
    )
    authorize_response.raise_for_status()
    authorize_res = authorize_response.json()
    response = await client.put(
        authorize_res.get('uploadUrl'),
        content=media_file_bytes,
        headers={'x-amz-meta-nvcf-asset-description': description, 'content-type': 'image/jpeg'},
        timeout=300,
    )

    response.raise_for_status()
    return UUID(authorize_res.get('assetId'))


async def delete_asset(client: AsyncClient, asset_id: UUID, api_key: str) -> None:
    headers = {'Authorization': f'Bearer {api_key}'}
    response = await client.delete(f'{ASSETS_URL}/{asset_id}', headers=headers, timeout=30)
    response.raise_for_status()


async def chat_with_media_nvcf(infer_url: str, media_file_bytes: bytes, query: str, api_key: str) -> str | None:
    try:
        async with AsyncClient(follow_redirects=True, timeout=45) as client:
            asset_list = []
            asset_id = await upload_asset(client, media_file_bytes, 'Reference media file', api_key)
            asset_list.append(str(asset_id))
            media_content = f'<img src="data:image/jpeg;asset_id,{asset_id}" />'
            asset_seq = ','.join(asset_list)
            headers = {
                'Authorization': f'Bearer {api_key}',
                'Content-Type': 'application/json',
                'NVCF-INPUT-ASSET-REFERENCES': asset_seq,
                'NVCF-FUNCTION-ASSET-IDS': asset_seq,
                'Accept': 'application/json',
            }

            payload = {
                'max_tokens': 1024,
                'temperature': 0.65,
                'top_p': 0.95,
                'seed': randint(0, 999999999),
                'messages': [{'role': 'user', 'content': f'{query} {media_content}'}],
                'stream': False,
                "model": infer_url.split('/v1/vlm/')[-1]
            }

            response = await client.post(infer_url, headers=headers, json=payload)
            response_json = response.json()
            answer = response_json.get('choices', [{}])[0].get('message', {}).get('content', None)
            for asset_id in asset_list:
                await delete_asset(client, UUID(asset_id), api_key)
            return answer
    except Exception as exc:
        print(exc)
        return None


def base64_to_jpeg_bytes(base64_str: str) -> bytes:
    try:
        if ',' not in base64_str:
            raise ValueError('недопустимый формат строки base64')
        base64_data = base64_str.split(',', 1)[1]
        binary_data = b64decode(base64_data)
        with image_open(BytesIO(binary_data)) as img:
            with BytesIO() as jpeg_bytes:
                img.convert('RGB').save(jpeg_bytes, format='JPEG', quality=90, optimize=True)
                return jpeg_bytes.getvalue()
    except (BinasciiError, OSError) as e:
        raise ValueError('данные не являются корректным изображением') from e


async def get_captions(image_base64_str: str, query: str, api_key: str) -> dict[str, str]:
    media_file_bytes = base64_to_jpeg_bytes(image_base64_str)
    tasks = [chat_with_media_nvcf(url, media_file_bytes, query, api_key) for url in INVOKE_URLS]
    results = await gather(*tasks)
    return dict(zip((url.split('/v1/vlm/')[-1] for url in INVOKE_URLS), results))


@asynccontextmanager
async def app_lifespan(_) -> AsyncGenerator:
    logger.info('запуск приложения')
    try:
        logger.info('старт API')
        yield
    finally:
        logger.info('приложение завершено')


app = FastAPI(lifespan=app_lifespan, title='NVIDIA_VLM_API')

banned_endpoints = [
    '/openapi.json',
    '/docs',
    '/docs/oauth2-redirect',
    'swagger_ui_redirect',
    '/redoc',
]


@app.middleware('http')
async def block_banned_endpoints(request: Request, call_next):
    logger.debug(f'получен запрос: {request.url.path}')
    if request.url.path in banned_endpoints:
        logger.warning(f'запрещенный endpoint: {request.url.path}')
        return Response(status_code=403)
    response = await call_next(request)
    return response


@app.post('/v1/describe')
async def describe_v1(request: Request):
    logger.info('запрос `describe_v1`')
    body = await request.json()
    headers = request.headers
    authorization: str = headers.get('Authorization') or headers.get('authorization')
    nvapi_key = authorization.removeprefix('Bearer ').strip()
    if not authorization or not nvapi_key:
        return JSONResponse({'caption': 'в запросе нужно передать заголовок `Authorization: Bearer <NVAPI_KEY>`'}, status_code=401)

    content_text = ''
    image_data = ''

    messages = body.get('messages', [])
    for message in messages:
        role = message.get('role')
        content = message.get('content')

        if role in ['system', 'user']:
            if isinstance(content, str):
                content_text += content + ' '
            elif isinstance(content, list):
                for item in content:
                    if item.get('type') == 'text':
                        content_text += item.get('text', '') + ' '
                    elif item.get('type') == 'image_url':
                        image_url = item.get('image_url', {})
                        url = image_url.get('url')
                        if url and url.startswith('data:image/'):
                            image_data = url
        image_data, content_text = image_data.strip(), content_text.strip()

    if not content_text or not image_data:
        return JSONResponse({'caption': 'изображение должно быть передано как строка base64 `data:image/jpeg;base64,{base64_img}` а также текст'}, status_code=400)
    try:
        return JSONResponse(await get_captions(image_data, content_text, nvapi_key), status_code=200)
    except Exception as e:
        return JSONResponse({'caption': str(e)}, status_code=500)


@app.get('/')
async def root():
    return HTMLResponse('ну пролапс, ну и что', status_code=200)


if __name__ == '__main__':
    from uvicorn import run as uvicorn_run

    logger.info('запуск сервера uvicorn')
    uvicorn_run(app, host='0.0.0.0', port=7860)