Update app.py
Browse files
app.py
CHANGED
|
@@ -74,139 +74,139 @@ app = pn.Column(
|
|
| 74 |
# app.servable()
|
| 75 |
|
| 76 |
|
| 77 |
-
ICON_URLS = {
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
async def random_url(_):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
@pn.cache
|
| 95 |
-
def load_processor_model(
|
| 96 |
-
|
| 97 |
-
) -> Tuple[CLIPProcessor, CLIPModel]:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
async def open_image_url(image_url: str) -> Image:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
async def process_inputs(class_names: List[str], image_url: str):
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# create widgets
|
| 167 |
-
randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 168 |
-
|
| 169 |
-
image_url = pn.widgets.TextInput(
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
)
|
| 173 |
-
class_names = pn.widgets.TextInput(
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
input_widgets = pn.Column(
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
)
|
| 184 |
-
|
| 185 |
-
# add interactivity
|
| 186 |
-
interactive_result = pn.panel(
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
# add footer
|
| 192 |
-
footer_row = pn.Row(pn.Spacer(), align="center")
|
| 193 |
-
for icon, url in ICON_URLS.items():
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
footer_row.append(pn.Spacer())
|
| 198 |
-
|
| 199 |
-
# create dashboard
|
| 200 |
-
main = pn.WidgetBox(
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
title = "Panel Demo - Image Classification"
|
| 207 |
-
pn.template.BootstrapTemplate(
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
).servable(title=title)
|
|
|
|
| 74 |
# app.servable()
|
| 75 |
|
| 76 |
|
| 77 |
+
# ICON_URLS = {
|
| 78 |
+
# "brand-github": "https://github.com/holoviz/panel",
|
| 79 |
+
# "brand-twitter": "https://twitter.com/Panel_Org",
|
| 80 |
+
# "brand-linkedin": "https://www.linkedin.com/company/panel-org",
|
| 81 |
+
# "message-circle": "https://discourse.holoviz.org/",
|
| 82 |
+
# "brand-discord": "https://discord.gg/AXRHnJU6sP",
|
| 83 |
+
# }
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# async def random_url(_):
|
| 87 |
+
# pet = random.choice(["cat", "dog"])
|
| 88 |
+
# api_url = f"https://api.the{pet}api.com/v1/images/search"
|
| 89 |
+
# async with aiohttp.ClientSession() as session:
|
| 90 |
+
# async with session.get(api_url) as resp:
|
| 91 |
+
# return (await resp.json())[0]["url"]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# @pn.cache
|
| 95 |
+
# def load_processor_model(
|
| 96 |
+
# processor_name: str, model_name: str
|
| 97 |
+
# ) -> Tuple[CLIPProcessor, CLIPModel]:
|
| 98 |
+
# processor = CLIPProcessor.from_pretrained(processor_name)
|
| 99 |
+
# model = CLIPModel.from_pretrained(model_name)
|
| 100 |
+
# return processor, model
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# async def open_image_url(image_url: str) -> Image:
|
| 104 |
+
# async with aiohttp.ClientSession() as session:
|
| 105 |
+
# async with session.get(image_url) as resp:
|
| 106 |
+
# return Image.open(io.BytesIO(await resp.read()))
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:
|
| 110 |
+
# processor, model = load_processor_model(
|
| 111 |
+
# "openai/clip-vit-base-patch32", "openai/clip-vit-base-patch32"
|
| 112 |
+
# )
|
| 113 |
+
# inputs = processor(
|
| 114 |
+
# text=class_items,
|
| 115 |
+
# images=[image],
|
| 116 |
+
# return_tensors="pt", # pytorch tensors
|
| 117 |
+
# )
|
| 118 |
+
# outputs = model(**inputs)
|
| 119 |
+
# logits_per_image = outputs.logits_per_image
|
| 120 |
+
# class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
|
| 121 |
+
# return class_likelihoods[0]
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# async def process_inputs(class_names: List[str], image_url: str):
|
| 125 |
+
# """
|
| 126 |
+
# High level function that takes in the user inputs and returns the
|
| 127 |
+
# classification results as panel objects.
|
| 128 |
+
# """
|
| 129 |
+
# try:
|
| 130 |
+
# main.disabled = True
|
| 131 |
+
# if not image_url:
|
| 132 |
+
# yield "##### β οΈ Provide an image URL"
|
| 133 |
+
# return
|
| 134 |
|
| 135 |
+
# yield "##### β Fetching image and running model..."
|
| 136 |
+
# try:
|
| 137 |
+
# pil_img = await open_image_url(image_url)
|
| 138 |
+
# img = pn.pane.Image(pil_img, height=400, align="center")
|
| 139 |
+
# except Exception as e:
|
| 140 |
+
# yield f"##### π Something went wrong, please try a different URL!"
|
| 141 |
+
# return
|
| 142 |
|
| 143 |
+
# class_items = class_names.split(",")
|
| 144 |
+
# class_likelihoods = get_similarity_scores(class_items, pil_img)
|
| 145 |
|
| 146 |
+
# # build the results column
|
| 147 |
+
# results = pn.Column("##### π Here are the results!", img)
|
| 148 |
|
| 149 |
+
# for class_item, class_likelihood in zip(class_items, class_likelihoods):
|
| 150 |
+
# row_label = pn.widgets.StaticText(
|
| 151 |
+
# name=class_item.strip(), value=f"{class_likelihood:.2%}", align="center"
|
| 152 |
+
# )
|
| 153 |
+
# row_bar = pn.indicators.Progress(
|
| 154 |
+
# value=int(class_likelihood * 100),
|
| 155 |
+
# sizing_mode="stretch_width",
|
| 156 |
+
# bar_color="secondary",
|
| 157 |
+
# margin=(0, 10),
|
| 158 |
+
# design=pn.theme.Material,
|
| 159 |
+
# )
|
| 160 |
+
# results.append(pn.Column(row_label, row_bar))
|
| 161 |
+
# yield results
|
| 162 |
+
# finally:
|
| 163 |
+
# main.disabled = False
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# # create widgets
|
| 167 |
+
# randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 168 |
+
|
| 169 |
+
# image_url = pn.widgets.TextInput(
|
| 170 |
+
# name="Image URL to classify",
|
| 171 |
+
# value=pn.bind(random_url, randomize_url),
|
| 172 |
+
# )
|
| 173 |
+
# class_names = pn.widgets.TextInput(
|
| 174 |
+
# name="Comma separated class names",
|
| 175 |
+
# placeholder="Enter possible class names, e.g. cat, dog",
|
| 176 |
+
# value="cat, dog, parrot",
|
| 177 |
+
# )
|
| 178 |
+
|
| 179 |
+
# input_widgets = pn.Column(
|
| 180 |
+
# "##### π Click randomize or paste a URL to start classifying!",
|
| 181 |
+
# pn.Row(image_url, randomize_url),
|
| 182 |
+
# class_names,
|
| 183 |
+
# )
|
| 184 |
+
|
| 185 |
+
# # add interactivity
|
| 186 |
+
# interactive_result = pn.panel(
|
| 187 |
+
# pn.bind(process_inputs, image_url=image_url, class_names=class_names),
|
| 188 |
+
# height=600,
|
| 189 |
+
# )
|
| 190 |
+
|
| 191 |
+
# # add footer
|
| 192 |
+
# footer_row = pn.Row(pn.Spacer(), align="center")
|
| 193 |
+
# for icon, url in ICON_URLS.items():
|
| 194 |
+
# href_button = pn.widgets.Button(icon=icon, width=35, height=35)
|
| 195 |
+
# href_button.js_on_click(code=f"window.open('{url}')")
|
| 196 |
+
# footer_row.append(href_button)
|
| 197 |
+
# footer_row.append(pn.Spacer())
|
| 198 |
+
|
| 199 |
+
# # create dashboard
|
| 200 |
+
# main = pn.WidgetBox(
|
| 201 |
+
# input_widgets,
|
| 202 |
+
# interactive_result,
|
| 203 |
+
# footer_row,
|
| 204 |
+
# )
|
| 205 |
+
|
| 206 |
+
# title = "Panel Demo - Image Classification"
|
| 207 |
+
# pn.template.BootstrapTemplate(
|
| 208 |
+
# title=title,
|
| 209 |
+
# main=main,
|
| 210 |
+
# main_max_width="min(50%, 698px)",
|
| 211 |
+
# header_background="#F08080",
|
| 212 |
+
# ).servable(title=title)
|