Victor.Katayama commited on
Commit
7b84d44
·
1 Parent(s): 43ef866

first commit

Browse files
Files changed (4) hide show
  1. README.md +9 -5
  2. app.py +45 -0
  3. dockerfile +10 -0
  4. requirements.txt +8 -0
README.md CHANGED
@@ -1,10 +1,14 @@
1
  ---
2
- title: Backend
3
- emoji: 😻
4
- colorFrom: indigo
5
- colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
1
  ---
2
+ title: Pokedex
3
+ emoji: 🐱
4
+ colorFrom: red
5
+ colorTo: blue
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
+ # Pokedex
11
+
12
+ Upload an image of a Pokemon and the model will identify it.
13
+
14
+ Model: skshmjn/Pokemon-classifier-gen9-1025
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from transformers import ViTForImageClassification, ViTImageProcessor
4
+ import torch
5
+ from PIL import Image
6
+ import io
7
+
8
+ app = FastAPI()
9
+
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=[
13
+ "http://localhost:5500",
14
+ "https://cosmicsheep42.github.io/Pokedex/",
15
+ "https://cosmicsheep42.github.io/Pokedex",
16
+ "https://cosmicsheep42-backend.hf.space"
17
+ ],
18
+ allow_credentials=True,
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
+ )
22
+
23
+ MAX_FILE_SIZE = 500 * 1024 * 1024 # 500MB
24
+
25
+ model_id = "skshmjn/Pokemon-classifier-gen9-1025"
26
+ model = ViTForImageClassification.from_pretrained(model_id)
27
+ processor = ViTImageProcessor.from_pretrained(model_id)
28
+
29
+ @app.post("/classify")
30
+ async def classify(file: UploadFile = File(...)):
31
+ if file.size and file.size > MAX_FILE_SIZE:
32
+ raise HTTPException(status_code=413, detail="File too large. Maximum size is 500MB.")
33
+
34
+ contents = await file.read()
35
+
36
+ if len(contents) > MAX_FILE_SIZE:
37
+ raise HTTPException(status_code=413, detail="File too large. Maximum size is 500MB.")
38
+
39
+ image = Image.open(io.BytesIO(contents)).convert("RGB")
40
+ inputs = processor(images=image, return_tensors="pt")
41
+ outputs = model(**inputs)
42
+ predicted_id = outputs.logits.argmax(-1).item()
43
+ pokemon_name = model.config.id2label[predicted_id]
44
+ return {"pokemonName": pokemon_name}
45
+
dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch
4
+ torchvision
5
+ transformers
6
+ pillow
7
+ python-multipart
8
+ spaces