Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, AutoTokenizer
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Load the pre-trained model
|
| 6 |
+
model_name = "facebook/deit-base-distilled-patch16-224-in21k"
|
| 7 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Create a function to classify an image
|
| 12 |
+
def classify_image(image):
|
| 13 |
+
inputs = tokenizer(image, return_tensors="pt")
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
predicted_class_idx = logits.argmax().item()
|
| 17 |
+
predicted_class = model.config.id2label[predicted_class_idx]
|
| 18 |
+
return predicted_class
|
| 19 |
+
|
| 20 |
+
# Create a Streamlit web app
|
| 21 |
+
st.title("AI Image Detector")
|
| 22 |
+
|
| 23 |
+
uploaded_image = st.file_uploader("Choose an image...")
|
| 24 |
+
if uploaded_image is not None:
|
| 25 |
+
image = Image.open(uploaded_image)
|
| 26 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 27 |
+
st.write("")
|
| 28 |
+
st.write("Classifying...")
|
| 29 |
+
|
| 30 |
+
predicted_class = classify_image(image)
|
| 31 |
+
|
| 32 |
+
st.subheader("Prediction:")
|
| 33 |
+
st.write(predicted_class)
|