rararara9999/testmodel
0
1import subprocess2 3# Install the required packages4subprocess.check_call(["pip", "install", "--upgrade", "pip"])5subprocess.check_call(["pip", "install", "-U", "transformers"])6subprocess.check_call(["pip", "install", "-U", "accelerate"])7subprocess.check_call(["pip", "install", "datasets"])8subprocess.check_call(["pip", "install", "evaluate"])9subprocess.check_call(["pip", "install", "scikit-learn"])10subprocess.check_call(["pip", "install", "torchvision"])11 12# Load model directly13from transformers import AutoTokenizer, AutoModelForSequenceClassification14 15tokenizer = AutoTokenizer.from_pretrained("rararara9999/Model")16model = AutoModelForSequenceClassification.from_pretrained("rararara9999/Model")17from transformers import AutoModelForImageClassification, AutoImageProcessor18 19import torch20import numpy as np21from PIL import Image22import streamlit as st23 24# Load the fine-tuned model and image processor25model_checkpoint = "rararara9999/Model"26model = AutoModelForImageClassification.from_pretrained(model_checkpoint, num_labels=2)27image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)28 29# Standalone Test Script30def test_model(image_path):31 # Load and preprocess the image32 image = Image.open(image_path)33 inputs = image_processor(images=image, return_tensors="pt")34 35 # Get model predictions36 outputs = model(**inputs)37 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)38 predictions = predictions.cpu().detach().numpy()39 40 # Get the index of the largest output value41 max_index = np.argmax(predictions)42 labels = ["Wearing Mask", "Not Wearing Mask"]43 predicted_label = labels[max_index]44 45 print(f"The predicted label is {predicted_label}")46 47# Streamlit App for Interactive Testing48def main():49 st.title("Face Mask Detection with HuggingFace Spaces")50 st.write("Upload an image to analyze whether the person is wearing a mask:")51 52 uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])53 if uploaded_file is not None:54 image = Image.open(uploaded_file)55 st.image(image, caption='Uploaded Image.', use_column_width=True)56 st.write("")57 st.write("Classifying...")58 59 # Preprocess the image60 inputs = image_processor(images=image, return_tensors="pt")61 62 # Get model predictions63 outputs = model(**inputs)64 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)65 predictions = predictions.cpu().detach().numpy()66 67 # Get the index of the largest output value68 max_index = np.argmax(predictions)69 labels = ["Wearing Mask", "Not Wearing Mask"]70 predicted_label = labels[max_index]71 confidence = predictions[max_index]72 73 st.write(f"Predicted Label: {predicted_label}")74 st.write(f"Confidence: {confidence:.2f}")75 76if __name__ == "__main__":77 main()78 