Spaces:
Running
Running
gui-update
#1
by
sergiopaniego
HF Staff
- opened
- .gitattributes +2 -0
- app.py +24 -23
- example_images/example_1.png +3 -0
- example_images/example_2.jpg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -8,18 +8,14 @@ import requests
|
|
| 8 |
from PIL import Image
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
-
# Hugging Face Inference Client (uses the free Inference
|
| 12 |
client = InferenceClient(model="Qwen/Qwen2.5-VL-32B-Instruct", provider="hf-inference")
|
| 13 |
|
| 14 |
BOX_TAG_PATTERN = r"<box>\((\d+),(\d+),(\d+),(\d+)\):([^<]+)</box>"
|
| 15 |
|
| 16 |
def parse_bounding_boxes(text: str) -> List[Tuple[Tuple[int, int, int, int], str]]:
|
| 17 |
-
"""Extract (bbox, label) pairs from model output."""
|
| 18 |
matches = re.findall(BOX_TAG_PATTERN, text)
|
| 19 |
-
|
| 20 |
-
for x1, y1, x2, y2, label in matches:
|
| 21 |
-
out.append(((int(x1), int(y1), int(x2), int(y2)), label.strip()))
|
| 22 |
-
return out
|
| 23 |
|
| 24 |
def fetch_image_from_url(url: str) -> Image.Image:
|
| 25 |
resp = requests.get(url, timeout=10)
|
|
@@ -32,15 +28,13 @@ def pil_to_data_uri(img: Image.Image) -> str:
|
|
| 32 |
return "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
|
| 33 |
|
| 34 |
def predict(image: Optional[Image.Image], image_url: str):
|
| 35 |
-
"""Run detection and return Gradio AnnotatedImage compatible output."""
|
| 36 |
if image is None and not image_url:
|
| 37 |
return None, "β Please provide an image or URL."
|
| 38 |
|
| 39 |
-
# Obtain PIL image + dataβURI for the API
|
| 40 |
if image is None:
|
| 41 |
try:
|
| 42 |
image = fetch_image_from_url(image_url)
|
| 43 |
-
data_uri = image_url
|
| 44 |
except Exception as e:
|
| 45 |
return None, f"β {e}"
|
| 46 |
else:
|
|
@@ -54,7 +48,6 @@ def predict(image: Optional[Image.Image], image_url: str):
|
|
| 54 |
"Do not include any other text or descriptions."
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# Call the inferenceΒ API (streaming)
|
| 58 |
stream = client.chat.completions.create(
|
| 59 |
messages=[
|
| 60 |
{"role": "user", "content": [
|
|
@@ -64,7 +57,6 @@ def predict(image: Optional[Image.Image], image_url: str):
|
|
| 64 |
],
|
| 65 |
stream=True,
|
| 66 |
)
|
| 67 |
-
|
| 68 |
response_text = "".join(chunk.choices[0].delta.content or "" for chunk in stream)
|
| 69 |
|
| 70 |
bboxes = parse_bounding_boxes(response_text)
|
|
@@ -79,27 +71,36 @@ def build_demo():
|
|
| 79 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="emerald")
|
| 80 |
with gr.Blocks(theme=theme, title="Qwen Object Detection Demo") as demo:
|
| 81 |
gr.Markdown("## Qwen2.5βVL Object Detection Demo π―")
|
| 82 |
-
gr.Markdown("Upload an image **or** paste an image URL, then click **Detect Objects
|
|
|
|
| 83 |
|
| 84 |
with gr.Tabs():
|
| 85 |
with gr.TabItem("Upload Image"):
|
| 86 |
img_input = gr.Image(type="pil", label="Upload Image", height=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
with gr.TabItem("Image URL"):
|
| 88 |
url_input = gr.Textbox(label="Image URL", placeholder="https://example.com/img.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
detect_btn = gr.Button("Detect Objects π")
|
| 91 |
-
output_img = gr.AnnotatedImage(label="Detections")
|
| 92 |
status = gr.Markdown()
|
| 93 |
|
| 94 |
-
gr.Examples(
|
| 95 |
-
examples=[
|
| 96 |
-
[None, "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/google-cloud/model-card.png"],
|
| 97 |
-
[None, "http://images.cocodataset.org/val2017/000000039769.jpg"],
|
| 98 |
-
],
|
| 99 |
-
inputs=[img_input, url_input],
|
| 100 |
-
label="Click an example to try π",
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
detect_btn.click(predict, inputs=[img_input, url_input], outputs=[output_img, status])
|
| 104 |
return demo
|
| 105 |
|
|
@@ -109,4 +110,4 @@ def main():
|
|
| 109 |
demo.launch()
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
main()
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
+
# Hugging Face Inference Client (uses the free Inference API)
|
| 12 |
client = InferenceClient(model="Qwen/Qwen2.5-VL-32B-Instruct", provider="hf-inference")
|
| 13 |
|
| 14 |
BOX_TAG_PATTERN = r"<box>\((\d+),(\d+),(\d+),(\d+)\):([^<]+)</box>"
|
| 15 |
|
| 16 |
def parse_bounding_boxes(text: str) -> List[Tuple[Tuple[int, int, int, int], str]]:
|
|
|
|
| 17 |
matches = re.findall(BOX_TAG_PATTERN, text)
|
| 18 |
+
return [((int(x1), int(y1), int(x2), int(y2)), label.strip()) for x1, y1, x2, y2, label in matches]
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def fetch_image_from_url(url: str) -> Image.Image:
|
| 21 |
resp = requests.get(url, timeout=10)
|
|
|
|
| 28 |
return "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
|
| 29 |
|
| 30 |
def predict(image: Optional[Image.Image], image_url: str):
|
|
|
|
| 31 |
if image is None and not image_url:
|
| 32 |
return None, "β Please provide an image or URL."
|
| 33 |
|
|
|
|
| 34 |
if image is None:
|
| 35 |
try:
|
| 36 |
image = fetch_image_from_url(image_url)
|
| 37 |
+
data_uri = image_url
|
| 38 |
except Exception as e:
|
| 39 |
return None, f"β {e}"
|
| 40 |
else:
|
|
|
|
| 48 |
"Do not include any other text or descriptions."
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
stream = client.chat.completions.create(
|
| 52 |
messages=[
|
| 53 |
{"role": "user", "content": [
|
|
|
|
| 57 |
],
|
| 58 |
stream=True,
|
| 59 |
)
|
|
|
|
| 60 |
response_text = "".join(chunk.choices[0].delta.content or "" for chunk in stream)
|
| 61 |
|
| 62 |
bboxes = parse_bounding_boxes(response_text)
|
|
|
|
| 71 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="emerald")
|
| 72 |
with gr.Blocks(theme=theme, title="Qwen Object Detection Demo") as demo:
|
| 73 |
gr.Markdown("## Qwen2.5βVL Object Detection Demo π―")
|
| 74 |
+
gr.Markdown("Upload an image **or** paste an image URL, then click **Detect Objects π**.")
|
| 75 |
+
gr.Markdown("[Check out the model](https://huggingface.co/Qwen/Qwen2.5-VL-32B-Instruct)")
|
| 76 |
|
| 77 |
with gr.Tabs():
|
| 78 |
with gr.TabItem("Upload Image"):
|
| 79 |
img_input = gr.Image(type="pil", label="Upload Image", height=300)
|
| 80 |
+
gr.Examples(
|
| 81 |
+
examples=[
|
| 82 |
+
["./example_images/example_1.png"],
|
| 83 |
+
["./example_images/example_2.jpg"],
|
| 84 |
+
],
|
| 85 |
+
inputs=[img_input],
|
| 86 |
+
label="Click an example to try π",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
with gr.TabItem("Image URL"):
|
| 90 |
url_input = gr.Textbox(label="Image URL", placeholder="https://example.com/img.jpg")
|
| 91 |
+
gr.Examples(
|
| 92 |
+
examples=[
|
| 93 |
+
[None, "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/google-cloud/model-card.png"],
|
| 94 |
+
[None, "http://images.cocodataset.org/val2017/000000039769.jpg"],
|
| 95 |
+
],
|
| 96 |
+
inputs=[img_input, url_input],
|
| 97 |
+
label="Click an example to try π",
|
| 98 |
+
)
|
| 99 |
|
| 100 |
detect_btn = gr.Button("Detect Objects π")
|
| 101 |
+
output_img = gr.AnnotatedImage(label="Detections", height=600)
|
| 102 |
status = gr.Markdown()
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
detect_btn.click(predict, inputs=[img_input, url_input], outputs=[output_img, status])
|
| 105 |
return demo
|
| 106 |
|
|
|
|
| 110 |
demo.launch()
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
main()
|
example_images/example_1.png
ADDED
|
Git LFS Details
|
example_images/example_2.jpg
ADDED
|
Git LFS Details
|