Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from fastapi import FastAPI, File, UploadFile | |
| from fastapi.responses import JSONResponse | |
| from PIL import Image | |
| import numpy as np | |
| import io | |
| import torch | |
| import torch.nn.functional as F | |
| from transformers import AutoImageProcessor, AutoModelForImageClassification | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| image_processor = AutoImageProcessor.from_pretrained("wesleyacheng/dog-breeds-multiclass-image-classification-with-vit") | |
| model = AutoModelForImageClassification.from_pretrained("wesleyacheng/dog-breeds-multiclass-image-classification-with-vit").to(device) | |
| labels = {int(k): v for k, v in model.config.id2label.items()} | |
| print(labels) | |
| # FastAPI ์ฑ ์์ฑ | |
| app = FastAPI() | |
| # ํ์ข ํ๊ธ ๋ฒ์ญ | |
| BREED_TRANSLATIONS = { | |
| "otterhound": "์คํฐํ์ด๋", "cocker_spaniel": "์ฝ์ปค ์คํจ๋์ผ", "brittany_spaniel": "๋ธ๋ฆฌํ๋ ์คํจ๋์ผ", | |
| "afghan_hound": "์ํ๊ฐ ํ์ด๋", "maltese_dog": "๋ชฐํฐ์ฆ", "schipperke": "์คํคํผํค", | |
| "irish_setter": "์์ด๋ฆฌ์ ์ธํฐ", "pekinese": "ํํค๋์ฆ", "golden_retriever": "๊ณจ๋ ๋ฆฌํธ๋ฆฌ๋ฒ", | |
| "vizsla": "๋น์ฆ๋ผ", "welsh_springer_spaniel": "์ฐ์ ์คํ๋ง๊ฑฐ ์คํจ๋์ผ", | |
| "staffordshire_bullterrier": "์คํํผ๋์ ๋ถํ ๋ฆฌ์ด", "border_collie": "๋ณด๋ ์ฝ๋ฆฌ", | |
| "irish_terrier": "์์ด๋ฆฌ์ ํ ๋ฆฌ์ด", "eskimo_dog": "์์คํค๋ชจ๊ฒฌ", "pug": "ํผ๊ทธ", | |
| "kelpie": "์ผํผ", "yorkshire_terrier": "์ํฌ์ ํ ๋ฆฌ์ด", "tibetan_terrier": "ํฐ๋ฒ ํ ํ ๋ฆฌ์ด", | |
| "walker_hound": "์์ปค ํ์ด๋", "affenpinscher": "์ํํ์ ", "cardigan": "์นด๋๊ฑด ์ฝ๊ธฐ", | |
| "english_springer": "์๊ธ๋ฆฌ์ ์คํ๋ง๊ฑฐ", "english_foxhound": "์๊ธ๋ฆฌ์ ํญ์คํ์ด๋", | |
| "west_highland_white_terrier": "์จ์คํธ ํ์ด๋๋ ํ์ดํธ ํ ๋ฆฌ์ด", "lakeland_terrier": "๋ ์ดํด๋๋ ํ ๋ฆฌ์ด", | |
| "rhodesian_ridgeback": "๋ก๋์ง์ ๋ฆฌ์ง๋ฐฑ", "gordon_setter": "๊ณ ๋ ์ธํฐ", "lhasa": "๋ผ์ฌ์์", | |
| "curly": "์ปฌ๋ฆฌ ์ฝํฐ๋ ๋ฆฌํธ๋ฆฌ๋ฒ", "beagle": "๋น๊ธ", "tibetan_mastiff": "ํฐ๋ฒ ํ ๋ง์คํฐํ", | |
| "sussex_spaniel": "์์์ค ์คํจ๋์ผ", "saint_bernard": "์ธ์ธํธ ๋ฒ๋๋", "toy_terrier": "ํ ์ด ํ ๋ฆฌ์ด", | |
| "standard_poodle": "์คํ ๋ค๋ ํธ๋ค", "bernese_mountain_dog": "๋ฒ๋์ฆ ๋ง์ดํด ๋ ", "pomeranian": "ํฌ๋ฉ๋ผ๋์", | |
| "ibizan_hound": "์ด๋น์ ํ์ด๋", "redbone": "๋ ๋๋ณธ ์ฟคํ์ด๋", "toy_poodle": "ํ ์ด ํธ๋ค", | |
| "basset": "๋ฐ์ ํ์ด๋", "scottish_deerhound": "์ค์ฝํฐ์ ๋์ดํ์ด๋", "miniature_pinscher": "๋ฏธ๋์ด์ฒ ํ์ ", | |
| "basenji": "๋ฐ์ผ์ง", "border_terrier": "๋ณด๋ ํ ๋ฆฌ์ด", "bedlington_terrier": "๋ฒ ๋ค๋งํด ํ ๋ฆฌ์ด", | |
| "kerry_blue_terrier": "์ผ๋ฆฌ ๋ธ๋ฃจ ํ ๋ฆฌ์ด", "weimaraner": "์์ด๋ง๋ผ๋", "english_setter": "์๊ธ๋ฆฌ์ ์ธํฐ", | |
| "bluetick": "๋ธ๋ฃจํฑ ์ฟคํ์ด๋", "boston_bull": "๋ณด์คํด ํ ๋ฆฌ์ด", "italian_greyhound": "์ดํ๋ฆฌ์ ๊ทธ๋ ์ดํ์ด๋", | |
| "dandie_dinmont": "๋๋ ๋๋ชฌํธ ํ ๋ฆฌ์ด", "airedale": "์์ด๋ฐ์ผ ํ ๋ฆฌ์ด", "irish_water_spaniel": "์์ด๋ฆฌ์ ์ํฐ ์คํจ๋์ผ", | |
| "norfolk_terrier": "๋ ธํฝ ํ ๋ฆฌ์ด", "wire": "์์ด์ด ํญ์ค ํ ๋ฆฌ์ด", "french_bulldog": "ํ๋ ์น ๋ถ๋ ", | |
| "soft": "์ํํธ ์ฝํฐ๋ ํํผ ํ ๋ฆฌ์ด", "komondor": "์ฝ๋ชฌ๋๋ฅด", "african_hunting_dog": "์ํ๋ฆฌ์นด ๋ค๊ฐ", | |
| "siberian_husky": "์๋ฒ ๋ฆฌ์ ํ์คํค", "newfoundland": "๋ดํ๋ค๋๋", "bouvier_des_flandres": "๋ถ๋น์ ๋ฐ ํ๋๋๋ฅด", | |
| "saluki": "์ด๋ฃจํค", "shetland_sheepdog": "์ ฐํ๋๋ ์ํ๋ ", "collie": "์ฝ๋ฆฌ", "rottweiler": "๋กํธ์์ผ๋ฌ", | |
| "silky_terrier": "์คํค ํ ๋ฆฌ์ด", "norwegian_elkhound": "๋ ธ๋ฅด์จ์ด ์ํฌํ์ด๋", "chihuahua": "์น์์", | |
| "leonberg": "๋ ์จ๋ฒ ๋ฅด๊ฑฐ", "norwich_terrier": "๋ ธ๋ฆฌ์น ํ ๋ฆฌ์ด", "cairn": "์ผ์ธ ํ ๋ฆฌ์ด", "boxer": "๋ณต์", | |
| "borzoi": "๋ณด๋ฅด์กฐ์ด", "dhole": "์น๋ฅ์ด", "samoyed": "์ฌ๋ชจ์๋", "german_shepherd": "์ ๋จผ ์ ฐํผ๋", | |
| "labrador_retriever": "๋๋ธ๋ผ๋ ๋ฆฌํธ๋ฆฌ๋ฒ", "blenheim_spaniel": "๋ธ๋ ๋ ์คํจ๋์ผ", "groenendael": "๊ทธ๋ก๋จ๋ฌ", | |
| "doberman": "๋๋ฒ ๋ฅด๋ง", "great_dane": "๊ทธ๋ ์ดํธ ๋ฐ์ธ", "flat": "ํ๋ซ ์ฝํฐ๋ ๋ฆฌํธ๋ฆฌ๋ฒ", | |
| "appenzeller": "์ํ์ ค๋ฌ", "shih": "์์ถ", "japanese_spaniel": "์ฌํจ๋์ฆ ์คํจ๋์ผ", | |
| "greater_swiss_mountain_dog": "๊ทธ๋ ์ดํฐ ์ค์์ค ๋ง์ดํด ๋ ", "black": "๋ธ๋ ์ค ํ ์ฟคํ์ด๋", | |
| "dingo": "๋ฉ๊ณ ", "great_pyrenees": "๊ทธ๋ ์ดํธ ํผ๋ ๋์ฆ", "whippet": "ํํซ", "keeshond": "ํค์คํผํธ", | |
| "malinois": "๋ง๋ฆฌ๋ ธ์ด์ฆ", "american_staffordshire_terrier": "์๋ฉ๋ฆฌ์นธ ์คํํผ๋์ ํ ๋ฆฌ์ด", | |
| "mexican_hairless": "๋ฉ์์นธ ํค์ด๋ฆฌ์ค", "giant_schnauzer": "์์ด์ธํธ ์๋์ฐ์ ", | |
| "brabancon_griffon": "๋ธ๋ผ๋ฐฉ์ก ๊ทธ๋ฆฌํ", "kuvasz": "์ฟ ๋ฐ์ค", "miniature_poodle": "๋ฏธ๋์ด์ฒ ํธ๋ค", | |
| "irish_wolfhound": "์์ด๋ฆฌ์ ์ธํํ์ด๋", "briard": "๋ธ๋ฆฌ์๋", "clumber": "ํด๋ผ๋ฒ ์คํจ๋์ผ", | |
| "standard_schnauzer": "์คํ ๋ค๋ ์๋์ฐ์ ", "bull_mastiff": "๋ถ ๋ง์คํฐํ", "malamute": "๋ง๋ผ๋ฎคํธ", | |
| "sealyham_terrier": "์ค๋ฆฌ์ ํ ๋ฆฌ์ด", "entlebucher": "์ํ๋ถ์ฒ", "chow": "์ฐจ์ฐ์ฐจ์ฐ", | |
| "papillon": "ํํผ์ฉ", "pembroke": "ํจ๋ธ๋กํฌ ์ฝ๊ธฐ", "german_short": "์ ๋จผ ์ผํธํค์ด๋ ํฌ์ธํฐ", | |
| "old_english_sheepdog": "์ฌ๋ ์๊ธ๋ฆฌ์ ์ํ๋ ", "chesapeake_bay_retriever": "์ฒด์ํผํฌ ๋ฒ ์ด ๋ฆฌํธ๋ฆฌ๋ฒ", | |
| "scotch_terrier": "์ค์นด์น ํ ๋ฆฌ์ด", "australian_terrier": "์ค์คํธ๋ ์ผ๋ฆฌ์ ํ ๋ฆฌ์ด", | |
| "miniature_schnauzer": "๋ฏธ๋์ด์ฒ ์๋์ฐ์ ", "bloodhound": "๋ธ๋ฌ๋ํ์ด๋" | |
| } | |
| def predict(image: Image.Image, k=10): | |
| inputs = image_processor(images=image, return_tensors="pt").to(device) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = F.softmax(outputs.logits, dim=-1)[0] | |
| top_probs, top_idxs = torch.topk(probs, k=k) | |
| top_probs = top_probs.cpu().tolist() | |
| top_idxs = top_idxs.cpu().tolist() | |
| # ์๋ฌธ ํ์ข ๋ช ์ ํ๊ธ๋ก ๋ฒ์ญํ์ฌ ๋ฐํ (ํ๋ฅ ์ 100 ๊ณฑํด์ ํผ์ผํธ๋ก) | |
| results = {} | |
| for i, prob in zip(top_idxs, top_probs): | |
| breed_name = labels[i] | |
| korean_name = BREED_TRANSLATIONS.get(breed_name, breed_name) | |
| results[korean_name] = round(float(prob) * 100, 3) | |
| return results | |
| def classify(profileImage: Image.Image): | |
| result = predict(profileImage) | |
| return result | |
| # FastAPI ์๋ํฌ์ธํธ: FormData๋ก ์ด๋ฏธ์ง ๋ฐ๊ธฐ | |
| async def classify_api(profileImage: UploadFile = File(...)): | |
| try: | |
| # ์ด๋ฏธ์ง ํ์ผ ์ฝ๊ธฐ | |
| contents = await profileImage.read() | |
| image = Image.open(io.BytesIO(contents)) | |
| # ์์ธก ์ํ | |
| result = predict(image) | |
| return JSONResponse(content={"success": True, "data": result}) | |
| except Exception as e: | |
| return JSONResponse( | |
| content={"success": False, "error": str(e)}, | |
| status_code=400 | |
| ) | |
| # Gradio ์ธํฐํ์ด์ค | |
| demo = gr.Interface(fn=classify, inputs=gr.Image(type="pil", label="profileImage"), outputs="label") | |
| # Gradio๋ฅผ FastAPI์ ๋ง์ดํธ | |
| app = gr.mount_gradio_app(app, demo, path="/") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |