Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,25 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
# URL del repositorio en Hugging Face
|
| 9 |
-
repo_id = "adwod/Streamlite_ViT_2000"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
config_url = hf_hub_url(filename="config.json", repo_id=repo_id)
|
| 13 |
-
# Descargar el archivo de configuraci贸n y cargarlo en una instancia de ViTConfig
|
| 14 |
-
config = ViTConfig.from_pretrained(config_url)
|
| 15 |
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Descargar el archivo de pesos del modelo y cargarlo en el modelo
|
| 20 |
-
model = ViTForImageClassification(config)
|
| 21 |
-
model.load_state_dict(torch.load(cached_download(model_path)))
|
| 22 |
-
|
| 23 |
-
# Cargar el extractor de caracter铆sticas
|
| 24 |
-
feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')
|
| 25 |
-
|
| 26 |
-
# Funci贸n para hacer predicciones en una imagen de entrada
|
| 27 |
-
def predict(image):
|
| 28 |
-
# Preprocesar la imagen
|
| 29 |
-
inputs = feature_extractor(image=image, return_tensors="pt")
|
| 30 |
-
# Hacer predicciones
|
| 31 |
-
outputs = model(**inputs)
|
| 32 |
-
# Obtener las etiquetas predichas
|
| 33 |
-
predicted_labels = torch.argmax(outputs.logits, dim=1)
|
| 34 |
-
# Devolver las etiquetas como una lista de strings
|
| 35 |
-
label_strings = ['inside', 'outside', 'food', 'drink', 'menu']
|
| 36 |
-
return [label_strings[label] for label in predicted_labels]
|
| 37 |
-
|
| 38 |
-
# Interfaz de usuario para cargar una imagen y hacer predicciones
|
| 39 |
-
st.title("ViT Image Classifier")
|
| 40 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 41 |
-
if uploaded_file is not None:
|
| 42 |
-
image = Image.open(uploaded_file)
|
| 43 |
-
st.image(image, caption='Uploaded image.', use_column_width=True)
|
| 44 |
-
predictions = predict(image)
|
| 45 |
-
st.write("Predicted labels:")
|
| 46 |
-
for label in predictions:
|
| 47 |
-
st.write(label)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoImageProcessor,
|
| 6 |
+
AutoModelForImageClassification
|
| 7 |
+
)
|
| 8 |
|
| 9 |
+
pre_process = AutoImageProcessor.from_pretrained('javierrf91/streamlit')
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
st.title("What is it?")
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
file_name = st.file_uploader("Upload a hot dog candidate image")
|
| 15 |
|
| 16 |
+
if file_name is not None:
|
| 17 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
image = Image.open(file_name)
|
| 20 |
+
col1.image(image, use_column_width=True)
|
| 21 |
+
inputs = pre_process(images=image, return_tensors="pt")
|
| 22 |
+
input_pixels = inputs.pixel_values
|
| 23 |
+
model = AutoModelForImageClassification.from_pretrained('javierrf91/streamlit')
|
| 24 |
+
outputs = model(input_pixels)
|
| 25 |
+
col2.header(model.config.id2label[outputs.logits.argmax(-1) .item()])
|