yeftakun commited on
Commit
83c18ee
1 Parent(s): 4ccfd35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -3,15 +3,12 @@ from transformers import ViTImageProcessor, AutoModelForImageClassification
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
 
 
6
 
7
  # Load the model and processor
8
- @st.cache_data
9
- def load_model():
10
- processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
11
- model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
12
- return processor, model
13
-
14
- processor, model = load_model()
15
 
16
  # Define prediction function
17
  def predict_image(image):
@@ -32,10 +29,13 @@ def predict_image(image):
32
  # Streamlit app
33
  st.title("NSFW Image Classifier")
34
 
35
- # Get image URL from query parameters
36
- query_params = st.query_params()
37
- image_url = query_params.get('image_url', [None])[0]
 
38
 
 
 
39
  if image_url:
40
  try:
41
  # Load image from URL
@@ -50,5 +50,28 @@ if image_url:
50
  st.write(f"Predicted Class: {prediction}")
51
  except Exception as e:
52
  st.write(f"Error: {e}")
53
- else:
54
- st.write("Please provide an image URL using the 'image_url' query parameter.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
6
+ import json
7
+ from flask import Flask, request, jsonify
8
 
9
  # Load the model and processor
10
+ processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
11
+ model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
 
 
 
 
 
12
 
13
  # Define prediction function
14
  def predict_image(image):
 
29
  # Streamlit app
30
  st.title("NSFW Image Classifier")
31
 
32
+ # Display API usage instructions
33
+ st.write("You can use this app with the API endpoint below. Send a POST request with the image URL to get classification.")
34
+ st.write("Example URL to use with curl:")
35
+ st.code("curl -X POST https://huggingface.co/spaces/yeftakun/nsfw_api2/api/classify -H 'Content-Type: application/json' -d '{\"image_url\": \"https://example.jpg\"}'")
36
 
37
+ # URL input for UI
38
+ image_url = st.text_input("Enter Image URL", placeholder="Enter image URL here")
39
  if image_url:
40
  try:
41
  # Load image from URL
 
50
  st.write(f"Predicted Class: {prediction}")
51
  except Exception as e:
52
  st.write(f"Error: {e}")
53
+
54
+ # API Endpoint using Flask
55
+ app = Flask(__name__)
56
+
57
+ @app.route('/api/classify', methods=['POST'])
58
+ def classify():
59
+ data = request.json
60
+ image_url = data.get('image_url')
61
+
62
+ if not image_url:
63
+ return jsonify({"error": "Image URL is required"}), 400
64
+
65
+ try:
66
+ # Load image from URL
67
+ response = requests.get(image_url)
68
+ image = Image.open(BytesIO(response.content))
69
+
70
+ # Predict image
71
+ prediction = predict_image(image)
72
+ return jsonify({"predicted_class": prediction})
73
+ except Exception as e:
74
+ return jsonify({"error": str(e)}), 500
75
+
76
+ if __name__ == '__main__':
77
+ app.run(port=5000)