Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,17 @@
|
|
1 |
-
import
|
2 |
import requests
|
3 |
import base64
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
|
13 |
api_key = "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"
|
14 |
-
|
15 |
|
16 |
def process_image(image_bytes, api_key):
|
17 |
header_auth = f"Bearer {api_key}"
|
@@ -44,20 +44,20 @@ def process_image(image_bytes, api_key):
|
|
44 |
'status': first_result.get('status', 'UNKNOWN')
|
45 |
}
|
46 |
|
47 |
-
|
48 |
return None
|
49 |
|
50 |
except requests.exceptions.RequestException as e:
|
51 |
-
|
52 |
return None
|
53 |
|
54 |
# File uploader
|
55 |
-
uploaded_file =
|
56 |
|
57 |
if uploaded_file is not None and api_key:
|
58 |
# Display the uploaded image
|
59 |
image = Image.open(uploaded_file)
|
60 |
-
|
61 |
|
62 |
# Convert image to bytes
|
63 |
img_byte_arr = io.BytesIO()
|
@@ -65,42 +65,42 @@ if uploaded_file is not None and api_key:
|
|
65 |
img_byte_arr = img_byte_arr.getvalue()
|
66 |
|
67 |
# Process the image
|
68 |
-
with
|
69 |
result = process_image(img_byte_arr, api_key)
|
70 |
|
71 |
if result and result['status'] == 'SUCCESS':
|
72 |
confidence = result['confidence']
|
73 |
sources = result['sources']
|
74 |
|
75 |
-
|
76 |
-
|
77 |
|
78 |
# Determine if image is AI-generated (using 50% threshold)
|
79 |
is_ai_generated = "Yes" if confidence >= 0.5 else "No"
|
80 |
|
81 |
# Display result with appropriate styling
|
82 |
if is_ai_generated == "Yes":
|
83 |
-
|
84 |
|
85 |
# Show top 3 possible sources if AI-generated
|
86 |
if sources:
|
87 |
-
|
88 |
sorted_sources = sorted(sources.items(), key=lambda x: x[1], reverse=True)[:3]
|
89 |
for source, prob in sorted_sources:
|
90 |
if prob > 0.01: # Only show sources with >1% probability
|
91 |
-
|
92 |
else:
|
93 |
-
|
94 |
|
95 |
# Show confidence score in smaller text
|
96 |
-
|
97 |
|
98 |
elif not api_key and uploaded_file is not None:
|
99 |
-
|
100 |
|
101 |
# Add footer with instructions
|
102 |
-
|
103 |
-
|
104 |
---
|
105 |
### How to use:
|
106 |
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
3 |
import base64
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
|
7 |
+
gr.set_page_config(page_title="AI Image Detector", page_icon="🔍")
|
8 |
|
9 |
+
gr.title("AI Image Detector")
|
10 |
+
gr.write("Upload an image to check if it's AI-generated")
|
11 |
|
12 |
|
13 |
api_key = "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"
|
14 |
+
gr.session_state.api_key = api_key
|
15 |
|
16 |
def process_image(image_bytes, api_key):
|
17 |
header_auth = f"Bearer {api_key}"
|
|
|
44 |
'status': first_result.get('status', 'UNKNOWN')
|
45 |
}
|
46 |
|
47 |
+
gr.error("Unexpected response format from API")
|
48 |
return None
|
49 |
|
50 |
except requests.exceptions.RequestException as e:
|
51 |
+
gr.error(f"Error processing image: {str(e)}")
|
52 |
return None
|
53 |
|
54 |
# File uploader
|
55 |
+
uploaded_file = gr.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
|
56 |
|
57 |
if uploaded_file is not None and api_key:
|
58 |
# Display the uploaded image
|
59 |
image = Image.open(uploaded_file)
|
60 |
+
gr.image(image, caption="Uploaded Image", use_container_width=True)
|
61 |
|
62 |
# Convert image to bytes
|
63 |
img_byte_arr = io.BytesIO()
|
|
|
65 |
img_byte_arr = img_byte_arr.getvalue()
|
66 |
|
67 |
# Process the image
|
68 |
+
with gr.spinner("Analyzing image..."):
|
69 |
result = process_image(img_byte_arr, api_key)
|
70 |
|
71 |
if result and result['status'] == 'SUCCESS':
|
72 |
confidence = result['confidence']
|
73 |
sources = result['sources']
|
74 |
|
75 |
+
gr.write("---")
|
76 |
+
gr.write("### Result")
|
77 |
|
78 |
# Determine if image is AI-generated (using 50% threshold)
|
79 |
is_ai_generated = "Yes" if confidence >= 0.5 else "No"
|
80 |
|
81 |
# Display result with appropriate styling
|
82 |
if is_ai_generated == "Yes":
|
83 |
+
gr.error(f"Is this image AI-generated? **{is_ai_generated}**")
|
84 |
|
85 |
# Show top 3 possible sources if AI-generated
|
86 |
if sources:
|
87 |
+
gr.write("Top possible AI models used:")
|
88 |
sorted_sources = sorted(sources.items(), key=lambda x: x[1], reverse=True)[:3]
|
89 |
for source, prob in sorted_sources:
|
90 |
if prob > 0.01: # Only show sources with >1% probability
|
91 |
+
gr.write(f"- {source}: {prob:.1%}")
|
92 |
else:
|
93 |
+
gr.success(f"Is this image AI-generated? **{is_ai_generated}**")
|
94 |
|
95 |
# Show confidence score in smaller text
|
96 |
+
gr.caption(f"Confidence score: {confidence:.2%}")
|
97 |
|
98 |
elif not api_key and uploaded_file is not None:
|
99 |
+
gr.warning("Please enter your NVIDIA API key first")
|
100 |
|
101 |
# Add footer with instructions
|
102 |
+
gr.markdown("---")
|
103 |
+
gr.markdown("""
|
104 |
---
|
105 |
### How to use:
|
106 |
|