Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Load model directly
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("rararara9999/Model")
|
| 5 |
+
model = AutoModelForSequenceClassification.from_pretrained("rararara9999/Model")
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
st.title("Face Mask Detection with HuggingFace Spaces")
|
| 13 |
+
st.write("Upload an image to analyze whether the person is wearing a mask:")
|
| 14 |
+
|
| 15 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 16 |
+
if uploaded_file is not None:
|
| 17 |
+
image = Image.open(uploaded_file)
|
| 18 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 19 |
+
st.write("")
|
| 20 |
+
st.write("Classifying...")
|
| 21 |
+
|
| 22 |
+
# Load the fine-tuned model and image processor
|
| 23 |
+
model_checkpoint = "rararara9999/Model"
|
| 24 |
+
image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
|
| 25 |
+
|
| 26 |
+
# Preprocess the image
|
| 27 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
| 28 |
+
|
| 29 |
+
# Get model predictions
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 32 |
+
predictions = predictions.cpu().detach().numpy()
|
| 33 |
+
|
| 34 |
+
# Get the index of the largest output value
|
| 35 |
+
max_index = np.argmax(predictions)
|
| 36 |
+
labels = ["Wearing Mask", "Not Wearing Mask"]
|
| 37 |
+
predicted_label = labels[max_index]
|
| 38 |
+
confidence = predictions[max_index]
|
| 39 |
+
|
| 40 |
+
st.write(f"Predicted Label: {predicted_label}")
|
| 41 |
+
st.write(f"Confidence: {confidence:.2f}")
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
main()
|