Skip to content

Commit 61284e0

Browse files
committed
switch to package implementation of 'is_image'
1 parent 5c52665 commit 61284e0

File tree

2 files changed

+2
-35
lines changed

2 files changed

+2
-35
lines changed

fastchat/serve/gradio_block_arena_vision.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,39 +74,6 @@ def get_vqa_sample():
7474
return (res, path)
7575

7676

77-
def is_image(file_path):
78-
magic_numbers = {
79-
b"\xff\xd8\xff": "JPEG",
80-
b"\x89PNG\r\n\x1a\n": "PNG",
81-
b"GIF87a": "GIF",
82-
b"GIF89a": "GIF",
83-
b"BM": "BMP",
84-
b"\x00\x00\x01\x00": "ICO",
85-
b"\x49\x49\x2a\x00": "TIFF",
86-
b"\x4d\x4d\x00\x2a": "TIFF",
87-
# For WebP, the first four bytes are "RIFF", but we also check for "WEBP"
88-
# in bytes 8–12.
89-
}
90-
91-
try:
92-
with open(file_path, "rb") as f:
93-
header = f.read(16) # Read a bit more to handle WebP safely
94-
95-
# Check for WebP (RIFF + WEBP)
96-
if header.startswith(b"RIFF") and header[8:12] == b"WEBP":
97-
return True
98-
99-
# Check other formats
100-
for magic in magic_numbers:
101-
if header.startswith(magic):
102-
return True
103-
104-
return False
105-
except Exception as e:
106-
print(f"Error reading file: {e}")
107-
return False
108-
109-
11077
def is_pdf(file_path):
11178
try:
11279
with open(file_path, "rb") as file:

fastchat/serve/gradio_block_arena_vision_anony.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import json
77
import time
8+
import filetype
89

910
import gradio as gr
1011
import numpy as np
@@ -71,7 +72,6 @@
7172
invisible_text,
7273
visible_text,
7374
disable_multimodal,
74-
is_image,
7575
is_pdf,
7676
)
7777
from fastchat.serve.gradio_global_state import Context
@@ -276,7 +276,7 @@ def add_text(
276276
states = [state0, state1]
277277
model_selectors = [model_selector0, model_selector1]
278278

279-
images = [file for file in files if is_image(file)]
279+
images = [file for file in files if filetype.is_image(file)]
280280
pdfs = [file for file in files if is_pdf(file)]
281281

282282
# Init states if necessary

0 commit comments

Comments
 (0)