from typing import Optional, List
from fastapi import FastAPI, File, UploadFile, Request
from sqlmodel import Field, Session, SQLModel, create_engine, select
from pydantic import BaseModel
from network import Network
import requests
from PIL import Image as ImagePIL
import torchvision as tv

app = FastAPI()
network = Network()
network.model.eval()


class Image(SQLModel, table=True):
    key: Optional[int] = Field(default=None, primary_key=True)
    image_name: str
    label: str
    image_url: str

engine = create_engine("sqlite:///image.db")

@app.get("/")
def read_images():
    with Session(engine) as session:
        statement = select(Image)
        images = session.exec(statement).all()
        return images

class Item(BaseModel):
    key: int
    label: str = ""

@app.post("/")
def update_heroes(item: Item):
    with Session(engine) as session:
        statement = select(Image).where(Image.key == item.key)
        results = session.exec(statement)
        image = results.one()
        image.label = item.label
        session.add(image)
        session.commit()

@app.get("/predict/{item}")
def predict(item: int):
    import torch
    import numpy as np
    with 