File size: 1,194 Bytes
b412667
 
533baa1
b412667
 
533baa1
bc782eb
b412667
 
 
 
 
bc782eb
 
 
 
 
b412667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc782eb
b412667
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import torch
import streamlit as st
from PIL import Image
from transformers import AutoModelForImageClassification, AutoImageProcessor


st.set_page_config(page_title="Turquoise Classification")

MAX_FILE_SIZE = 5 * 1024 * 1024  # 5MB

st.write("## Real or fake turquoise?")

st.write(
    ":dog: Try uploading an image of a turquoise to check :grin:"
)

repo_name = "dzhao114/vit-base-patch16-224-finetuned-turquoise"

image_processor = AutoImageProcessor.from_pretrained(repo_name)

model = AutoModelForImageClassification.from_pretrained(repo_name)

def inference(upload):
	image = Image.open(upload)
	encoding = image_processor(image.convert("RGB"), return_tensors="pt")
	st.image(image)

	with torch.no_grad():
		outputs = model(**encoding)
		logits = outputs.logits

	predicted_class_idx = logits.argmax(-1).item()
	st.write("Predicted:", model.config.id2label[predicted_class_idx])


my_upload = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

if my_upload is not None:
    if my_upload.size > MAX_FILE_SIZE:
        st.error("The uploaded file is too large. Please upload an image smaller than 5MB.")
    else:
        inference(my_upload)

else:
	pass