yeftakun commited on
Commit
fffae6d
1 Parent(s): c4ce120

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -53
app.py CHANGED
@@ -3,7 +3,6 @@ from transformers import ViTImageProcessor, AutoModelForImageClassification
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
6
- import json
7
 
8
  # Load the model and processor
9
  processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
@@ -25,57 +24,17 @@ def predict_image(image):
25
  except Exception as e:
26
  return str(e)
27
 
28
- # Streamlit app for UI and API endpoint
29
  st.title("NSFW Image Classifier")
30
 
31
- # URL input for UI
32
- image_url_ui = st.text_input("Enter Image URL", placeholder="Enter image URL here")
33
-
34
- # API endpoint for classification (POST request)
35
- @st.experimental_singleton # Ensure a single instance for performance
36
- def api_endpoint():
37
- if request.method == 'POST':
38
- data = request.json
39
- if 'image_url' in data:
40
- try:
41
- image_url = data['image_url']
42
- # Load image from URL
43
- response = requests.get(image_url)
44
- image = Image.open(BytesIO(response.content))
45
-
46
- # Predict and return result as JSON
47
- prediction = predict_image(image)
48
- return json.dumps({'predicted_class': prediction})
49
- except Exception as e:
50
- return json.dumps({'error': str(e)}), 500 # Internal Server Error
51
- else:
52
- return json.dumps({'error': 'Missing "image_url" in request body'}), 400 # Bad Request
53
- else:
54
- return json.dumps({'error': 'Only POST requests are allowed'}), 405 # Method Not Allowed
55
-
56
- st.experimental_next_router(api_endpoint) # Register the API endpoint
57
-
58
- if image_url_ui:
59
- try:
60
- # Load image from UI input (if URL is provided)
61
- response = requests.get(image_url_ui)
62
- image = Image.open(BytesIO(response.content))
63
- st.image(image, caption='Image from URL', use_column_width=True)
64
- st.write("")
65
- st.write("Classifying...")
66
-
67
- # Predict and display result (for UI)
68
- prediction = predict_image(image)
69
- st.write(f"Predicted Class: {prediction}")
70
- except Exception as e:
71
- st.write(f"Error: {e}")
72
-
73
- # Display API endpoint information
74
- space_url = st.session_state.get('huggingface_space_url') # Assuming it's available
75
- if space_url:
76
- api_endpoint_url = f"{space_url}/api/classify" # Construct the URL based on Space URL
77
- st.write(f"You can also use this API endpoint to classify images:")
78
- st.write(f"```curl")
79
- st.write(f"curl -X POST -H 'Content-Type: application/json' -d '{{ \"image_url\": \"https://example.jpg\" }}' {api_endpoint_url}")
80
- st.write(f"```")
81
- st.write(f"This will return the predicted class in JSON format.")
 
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
 
6
 
7
  # Load the model and processor
8
  processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
 
24
  except Exception as e:
25
  return str(e)
26
 
27
+ # Streamlit app
28
  st.title("NSFW Image Classifier")
29
 
30
+ # Upload image file
31
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
32
+ if uploaded_file is not None:
33
+ image = Image.open(uploaded_file)
34
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
35
+ st.write("")
36
+ st.write("Classifying...")
37
+
38
+ # Predict and display result
39
+ prediction = predict_image(image)
40
+ st.write(f"Predicted Class: {prediction}")