Download app.py from ruzu/cool-wall: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/spaces/ruzu/cool-wall/resolve/main/app.py
- Command line
-
hf download hf://spaces/ruzu/cool-wall/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ruzu/cool-wall/resolve/main/app.py
2.05 kB
| import os | |
| import random | |
| import gradio as gr | |
| from fastai.vision.all import load_learner, PILImage | |
| from huggingface_hub import from_pretrained_fastai | |
| def get_grandparent_label(o): | |
| return o.parent.parent.name | |
| #learn = load_learner('convnext_tiny.pkl') | |
| learn = from_pretrained_fastai("ruzu/coolwall") | |
| ratings = ('SU', 'U', 'C', 'SZ') | |
| def rate_car(img): | |
| img = img.resize((400, 400)) | |
| pred, idx, probs = learn.predict(img) | |
| label_map = {'SU': 'Super Uncool', 'U': 'Uncool', 'C':'Cool', 'SZ': 'Sub Zero'} | |
| result = {} | |
| for rating, prob in zip(ratings, map(float, probs)): | |
| display_rating = label_map.get(rating, rating) | |
| result[display_rating] = prob | |
| return result | |
| def get_random_file_from_each_dir(root_dir, num_samples=20): | |
| image_paths = [] | |
| for dirpath, dirnames, filenames in os.walk(root_dir): | |
| if os.path.relpath(dirpath, root_dir).count(os.sep) != 1: | |
| continue # only include cars/*/* directories | |
| files = [f for f in filenames if not f.startswith('.')] | |
| for f in files: | |
| image_paths.append(os.path.join(dirpath, f)) | |
| return [[p] for p in random.sample(image_paths, min(num_samples, len(image_paths)))] | |
| def get_examples(): | |
| return get_random_file_from_each_dir('cars') | |
| with gr.Blocks() as intf: | |
| with gr.Row(): | |
| image = gr.Image(type="pil", label="Upload an image") | |
| label = gr.Label(label="Cool Wall Rating") | |
| with gr.Row(): | |
| submit_button = gr.Button("Submit", interactive=False) | |
| clear_button = gr.Button("Clear") | |
| def enable_submit(img): | |
| return gr.update(interactive=img is not None) | |
| def clear_inputs(): | |
| return None, None, gr.update(interactive=False) | |
| image.change(enable_submit, inputs=image, outputs=submit_button) | |
| submit_button.click(rate_car, inputs=image, outputs=label) | |
| clear_button.click(clear_inputs, outputs=[image, label, submit_button]) | |
| gr.Examples( | |
| examples=get_examples(), | |
| inputs=image, | |
| examples_per_page=10 | |
| ) | |
| intf.launch() | |