prithivMLmods commited on
Commit
f79c9e0
·
verified ·
1 Parent(s): c0bfe5a

update app

Browse files
Files changed (1) hide show
  1. app.py +74 -17
app.py CHANGED
@@ -4,6 +4,77 @@ import gradio as gr
4
  from PIL import Image
5
  import requests
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  model = AutoModel.from_pretrained("facebook/metaclip-2-mt5-worldwide-s16", torch_dtype=torch.bfloat16, attn_implementation="sdpa")
8
  processor = AutoProcessor.from_pretrained("facebook/metaclip-2-mt5-worldwide-s16")
9
 
@@ -26,10 +97,10 @@ def infer(image, candidate_labels):
26
  probs = metaclip_detector(image, candidate_labels)
27
  return postprocess_metaclip(probs, labels=candidate_labels)
28
 
29
- with gr.Blocks() as demo:
30
- gr.Markdown("# MetaCLIP 2 Zero-Shot Classification")
31
  gr.Markdown(
32
- "Test the performance of MetaCLIP 2 on zero-shot classification in this Space :point_down:"
33
  )
34
  with gr.Row():
35
  with gr.Column():
@@ -39,20 +110,6 @@ with gr.Blocks() as demo:
39
  with gr.Column():
40
  metaclip_output = gr.Label(label="MetaCLIP 2 Output", num_top_classes=3)
41
 
42
- # It's recommended to have local images for the examples
43
- # For demonstration purposes, we will download them if they don't exist.
44
- def download_image(url, filename):
45
- import os
46
- if not os.path.exists(filename):
47
- response = requests.get(url, stream=True)
48
- response.raise_for_status()
49
- with open(filename, 'wb') as f:
50
- for chunk in response.iter_content(chunk_size=8192):
51
- f.write(chunk)
52
-
53
- download_image("https://gradio-builds.s3.amazonaws.com/demo-files/baklava.jpg", "baklava.jpg")
54
- download_image("https://gradio-builds.s3.amazonaws.com/demo-files/cat.jpg", "cat.jpg")
55
-
56
  examples = [
57
  ["./baklava.jpg", "dessert on a plate, a serving of baklava, a plate and spoon"],
58
  ["./cat.jpg", "a cat, two cats, three cats"],
 
4
  from PIL import Image
5
  import requests
6
 
7
+ from gradio.themes import Soft
8
+ from gradio.themes.utils import colors, fonts, sizes
9
+
10
+ colors.orange_red = colors.Color(
11
+ name="orange_red",
12
+ c50="#FFF0E5",
13
+ c100="#FFE0CC",
14
+ c200="#FFC299",
15
+ c300="#FFA366",
16
+ c400="#FF8533",
17
+ c500="#FF4500",
18
+ c600="#E63E00",
19
+ c700="#CC3700",
20
+ c800="#B33000",
21
+ c900="#992900",
22
+ c950="#802200",
23
+ )
24
+
25
+ class OrangeRedTheme(Soft):
26
+ def __init__(
27
+ self,
28
+ *,
29
+ primary_hue: colors.Color | str = colors.gray,
30
+ secondary_hue: colors.Color | str = colors.orange_red, # Use the new color
31
+ neutral_hue: colors.Color | str = colors.slate,
32
+ text_size: sizes.Size | str = sizes.text_lg,
33
+ font: fonts.Font | str | Iterable[fonts.Font | str] = (
34
+ fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
35
+ ),
36
+ font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
37
+ fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
38
+ ),
39
+ ):
40
+ super().__init__(
41
+ primary_hue=primary_hue,
42
+ secondary_hue=secondary_hue,
43
+ neutral_hue=neutral_hue,
44
+ text_size=text_size,
45
+ font=font,
46
+ font_mono=font_mono,
47
+ )
48
+ super().set(
49
+ background_fill_primary="*primary_50",
50
+ background_fill_primary_dark="*primary_900",
51
+ body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
52
+ body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
53
+ button_primary_text_color="white",
54
+ button_primary_text_color_hover="white",
55
+ button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
56
+ button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
57
+ button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)",
58
+ button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)",
59
+ button_secondary_text_color="black",
60
+ button_secondary_text_color_hover="white",
61
+ button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
62
+ button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
63
+ button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
64
+ button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
65
+ slider_color="*secondary_500",
66
+ slider_color_dark="*secondary_600",
67
+ block_title_text_weight="600",
68
+ block_border_width="3px",
69
+ block_shadow="*shadow_drop_lg",
70
+ button_primary_shadow="*shadow_drop_lg",
71
+ button_large_padding="11px",
72
+ color_accent_soft="*primary_100",
73
+ block_label_background_fill="*primary_200",
74
+ )
75
+
76
+ orange_red_theme = OrangeRedTheme()
77
+
78
  model = AutoModel.from_pretrained("facebook/metaclip-2-mt5-worldwide-s16", torch_dtype=torch.bfloat16, attn_implementation="sdpa")
79
  processor = AutoProcessor.from_pretrained("facebook/metaclip-2-mt5-worldwide-s16")
80
 
 
97
  probs = metaclip_detector(image, candidate_labels)
98
  return postprocess_metaclip(probs, labels=candidate_labels)
99
 
100
+ with gr.Blocks(theme=orange_red_theme) as demo:
101
+ gr.Markdown("# **MetaCLIP 2 Zero-Shot Classification**")
102
  gr.Markdown(
103
+ "Test the performance of MetaCLIP 2 on zero-shot classification in this Space"
104
  )
105
  with gr.Row():
106
  with gr.Column():
 
110
  with gr.Column():
111
  metaclip_output = gr.Label(label="MetaCLIP 2 Output", num_top_classes=3)
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  examples = [
114
  ["./baklava.jpg", "dessert on a plate, a serving of baklava, a plate and spoon"],
115
  ["./cat.jpg", "a cat, two cats, three cats"],