Skip to content

Commit cf9b408

Browse files
author
PranavB-11
committed
Content Moderation implemented + couple of latency improvements
1 parent f2c4d64 commit cf9b408

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

fastchat/serve/gradio_block_arena_vision.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,21 +273,26 @@ def wrap_pdfchat_query(query, document):
273273

274274
def convert_base64_to_pil_image(b64_string):
275275
from PIL import Image
276+
import numpy as np
276277

277-
image_data = base64.b64decode(b64_string)
278+
image_data = np.frombuffer(base64.b64decode(b64_string), dtype=np.uint8)
278279
image_bytes = BytesIO(image_data)
279280
image = Image.open(image_bytes)
280281

281282
return image
282283

284+
def batch_convert_base64_to_images(base64_dict):
285+
import concurrent.futures
286+
with concurrent.futures.ThreadPoolExecutor() as executor:
287+
return list(executor.map(convert_base64_to_pil_image, base64_dict.values()))
288+
283289
def parse_pdf(file_path):
284290
import requests
285291

286292
url = "https://www.datalab.to/api/v1/marker"
287293

288294
form_data = {
289295
'file': ('test.pdf', open(file_path, 'rb'), 'application/pdf'),
290-
'langs': (None, "English"),
291296
"force_ocr": (None, False),
292297
"paginate": (None, False),
293298
'output_format': (None, 'markdown'),
@@ -296,7 +301,7 @@ def parse_pdf(file_path):
296301
"disable_image_extraction": (None, False)
297302
}
298303

299-
headers = {"X-Api-Key": os.getenv("X-Api-Key")}
304+
headers = {"X-Api-Key": str(os.getenv("MARKER_API_KEY"))}
300305
response = requests.post(url, files=form_data, headers=headers)
301306
data = response.json()
302307

@@ -312,7 +317,7 @@ def parse_pdf(file_path):
312317
break
313318

314319
output_md = data["markdown"]
315-
output_images = [convert_base64_to_pil_image(b64_image) for b64_image in data["images"].values()]
320+
output_images = batch_convert_base64_to_images(data["images"])
316321

317322
return output_md, output_images
318323

fastchat/serve/gradio_block_arena_vision_anony.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,6 @@ def add_text(
312312
State(model_left, is_vision=False, pdf_id=unique_id),
313313
State(model_right, is_vision=False, pdf_id=unique_id),
314314
]
315-
upload_pdf_file_to_gcs(
316-
pdf_file_path=pdfs[0],
317-
filename=unique_id,
318-
)
319315
else:
320316
model_left, model_right = get_battle_pair(
321317
context.all_text_models,
@@ -366,10 +362,17 @@ def add_text(
366362

367363
images = convert_images_to_conversation_format(images)
368364

369-
# TODO: add PDF moderator
370-
text, image_flagged, csam_flag = moderate_input(
371-
state0, text, text, model_list, images, ip
372-
)
365+
post_processed_text = _prepare_text_with_pdf(text[:BLIND_MODE_INPUT_CHAR_LEN_LIMIT], pdfs)
366+
if type(post_processed_text) is tuple:
367+
text += post_processed_text[0]
368+
text, image_flagged, csam_flag = moderate_input(
369+
state0, text, text, model_list, images + post_processed_text[1], ip
370+
)
371+
else:
372+
text += post_processed_text
373+
text, image_flagged, csam_flag = moderate_input(
374+
state0, text, text, model_list, images, ip
375+
)
373376

374377
conv = states[0].conv
375378
if (len(conv.messages) - conv.offset) // 2 >= CONVERSATION_TURN_LIMIT:
@@ -408,7 +411,11 @@ def add_text(
408411
)
409412

410413
text = text[:BLIND_MODE_INPUT_CHAR_LEN_LIMIT] # Hard cut-off
411-
post_processed_text = _prepare_text_with_pdf(text, pdfs)
414+
415+
upload_pdf_file_to_gcs(
416+
pdf_file_path=pdfs[0],
417+
filename=unique_id,
418+
)
412419

413420
for i in range(num_sides):
414421
post_processed_text = _prepare_text_with_image(

0 commit comments

Comments
 (0)