philippjfr sophiamyang commited on
Commit
93607a7
β€’
0 Parent(s):

Duplicate from Panel-Org/panel-template

Browse files

Co-authored-by: Sophia Yang <sophiamyang@users.noreply.huggingface.co>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. Dockerfile +16 -0
  3. README.md +11 -0
  4. app.py +141 -0
  5. requirements.txt +6 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+ RUN python3 -m pip install --no-cache-dir --upgrade pip
7
+ RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*"]
12
+
13
+ RUN mkdir /.cache
14
+ RUN chmod 777 /.cache
15
+ RUN mkdir .chroma
16
+ RUN chmod 777 .chroma
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Panel Template
3
+ emoji: πŸ“ˆ
4
+ colorFrom: gray
5
+ colorTo: green
6
+ sdk: docker
7
+ pinned: false
8
+ duplicated_from: Panel-Org/panel-template
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import random
3
+ from typing import List, Tuple
4
+
5
+ import aiohttp
6
+ import panel as pn
7
+ from PIL import Image
8
+ from transformers import CLIPModel, CLIPProcessor
9
+
10
+ pn.extension(design="bootstrap", sizing_mode="stretch_width")
11
+
12
+ ICON_URLS = {
13
+ "brand-github": "https://github.com/holoviz/panel",
14
+ "brand-twitter": "https://twitter.com/Panel_Org",
15
+ "brand-linkedin": "https://www.linkedin.com/company/panel-org",
16
+ "message-circle": "https://discourse.holoviz.org/",
17
+ "brand-discord": "https://discord.gg/AXRHnJU6sP",
18
+ }
19
+
20
+
21
+ async def random_url(_):
22
+ pet = random.choice(["cat", "dog"])
23
+ api_url = f"https://api.the{pet}api.com/v1/images/search"
24
+ async with aiohttp.ClientSession() as session:
25
+ async with session.get(api_url) as resp:
26
+ return (await resp.json())[0]["url"]
27
+
28
+
29
+ @pn.cache
30
+ def load_processor_model(
31
+ processor_name: str, model_name: str
32
+ ) -> Tuple[CLIPProcessor, CLIPModel]:
33
+ processor = CLIPProcessor.from_pretrained(processor_name)
34
+ model = CLIPModel.from_pretrained(model_name)
35
+ return processor, model
36
+
37
+
38
+ async def open_image_url(image_url: str) -> Image:
39
+ async with aiohttp.ClientSession() as session:
40
+ async with session.get(image_url) as resp:
41
+ return Image.open(io.BytesIO(await resp.read()))
42
+
43
+
44
+ def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:
45
+ processor, model = load_processor_model(
46
+ "openai/clip-vit-base-patch32", "openai/clip-vit-base-patch32"
47
+ )
48
+ inputs = processor(
49
+ text=class_items,
50
+ images=[image],
51
+ return_tensors="pt", # pytorch tensors
52
+ )
53
+ outputs = model(**inputs)
54
+ logits_per_image = outputs.logits_per_image
55
+ class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
56
+ return class_likelihoods[0]
57
+
58
+
59
+ async def process_inputs(class_names: List[str], image_url: str):
60
+ """
61
+ High level function that takes in the user inputs and returns the
62
+ classification results as panel objects.
63
+ """
64
+ main.disabled = True
65
+ if not image_url:
66
+ yield "##### ⚠️ Provide an image URL"
67
+ return
68
+
69
+ yield "##### βš™ Fetching image and running model..."
70
+ pil_img = await open_image_url(image_url)
71
+ img = pn.pane.Image(pil_img, height=400, align="center")
72
+
73
+ class_items = class_names.split(",")
74
+ class_likelihoods = get_similarity_scores(class_items, pil_img)
75
+
76
+ # build the results column
77
+ results = pn.Column("##### πŸŽ‰ Here are the results!", img)
78
+
79
+ for class_item, class_likelihood in zip(class_items, class_likelihoods):
80
+ row_label = pn.widgets.StaticText(
81
+ name=class_item.strip(), value=f"{class_likelihood:.2%}", align="center"
82
+ )
83
+ row_bar = pn.indicators.Progress(
84
+ value=int(class_likelihood * 100),
85
+ sizing_mode="stretch_width",
86
+ bar_color="secondary",
87
+ margin=(0, 10),
88
+ design=pn.theme.Material,
89
+ )
90
+ results.append(pn.Column(row_label, row_bar))
91
+ main.disabled = False
92
+ yield results
93
+
94
+
95
+ # create widgets
96
+ randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
97
+
98
+ image_url = pn.widgets.TextInput(
99
+ name="Image URL to classify",
100
+ value=pn.bind(random_url, randomize_url),
101
+ )
102
+ class_names = pn.widgets.TextInput(
103
+ name="Comma separated class names",
104
+ placeholder="Enter possible class names, e.g. cat, dog",
105
+ value="cat, dog, parrot",
106
+ )
107
+
108
+ input_widgets = pn.Column(
109
+ "##### 😊 Click randomize or paste a URL to start classifying!",
110
+ pn.Row(image_url, randomize_url),
111
+ class_names,
112
+ )
113
+
114
+ # add interactivity
115
+ interactive_result = pn.panel(
116
+ pn.bind(process_inputs, image_url=image_url, class_names=class_names),
117
+ height=600,
118
+ )
119
+
120
+ # add footer
121
+ footer_row = pn.Row(pn.Spacer(), align="center")
122
+ for icon, url in ICON_URLS.items():
123
+ href_button = pn.widgets.Button(icon=icon, width=35, height=35)
124
+ href_button.js_on_click(code=f"window.open('{url}')")
125
+ footer_row.append(href_button)
126
+ footer_row.append(pn.Spacer())
127
+
128
+ # create dashboard
129
+ main = pn.WidgetBox(
130
+ input_widgets,
131
+ interactive_result,
132
+ footer_row,
133
+ )
134
+
135
+ title = "Panel Demo - Image Classification"
136
+ pn.template.BootstrapTemplate(
137
+ title=title,
138
+ main=main,
139
+ main_max_width="min(50%, 698px)",
140
+ header_background="#F08080",
141
+ ).servable(title=title)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ panel
2
+ jupyter
3
+ transformers
4
+ numpy
5
+ torch
6
+ aiohttp