Spaces:
Running
Running
Update pmo_func.py
Browse files- pmo_func.py +20 -17
pmo_func.py
CHANGED
|
@@ -224,7 +224,7 @@ class FactChecker:
|
|
| 224 |
|
| 225 |
print("No FactCheck result, falling back to live web analysis...")
|
| 226 |
return self.search_and_analyze_claim(claim)
|
| 227 |
-
|
| 228 |
class img_manipulation:
|
| 229 |
def __init__(self):
|
| 230 |
self.GEN_AI_IMAGE = pipeline("image-classification", model="umm-maybe/AI-image-detector", device=DEVICE)
|
|
@@ -240,37 +240,40 @@ class img_manipulation:
|
|
| 240 |
print(f'AI image detection error: {e}')
|
| 241 |
return 0.0
|
| 242 |
|
| 243 |
-
def generated_image(self, img_pth, quality=90
|
|
|
|
|
|
|
|
|
|
| 244 |
try:
|
| 245 |
with Image.open(img_pth) as orig_img:
|
| 246 |
orig_img = orig_img.convert('RGB')
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
ela_image = ImageChops.difference(orig_img, resaved_img)
|
| 251 |
-
|
| 252 |
ela_data = np.array(ela_image)
|
| 253 |
mean_intensity = ela_data.mean()
|
| 254 |
scaled_score = min(100, (mean_intensity / 25.0) * 100)
|
|
|
|
| 255 |
|
| 256 |
-
# Save the ELA image and return its path for serving
|
| 257 |
-
ela_path = "ela_result.png"
|
| 258 |
-
enhancer = ImageEnhance.Brightness(ela_image)
|
| 259 |
-
max_diff = max(1, max([ex[1] for ex in ela_image.getextrema()]))
|
| 260 |
-
ela_image_enhanced = enhancer.enhance(scale / max_diff)
|
| 261 |
-
ela_image_enhanced.save(ela_path)
|
| 262 |
-
return scaled_score, ela_path
|
| 263 |
except Exception as e:
|
| 264 |
-
print(f'ELA
|
| 265 |
-
return 0.0
|
| 266 |
|
| 267 |
def run_image_forensics(self, image_path):
|
| 268 |
ai_score = self.Gen_AI_IMG(image_path)
|
| 269 |
-
classic_score
|
|
|
|
|
|
|
| 270 |
return {
|
| 271 |
"ai_generated_score_percent": ai_score,
|
| 272 |
"classic_edit_score_percent": classic_score,
|
| 273 |
-
"ela_image_path": ela_path
|
| 274 |
}
|
| 275 |
|
| 276 |
class OCR:
|
|
|
|
| 224 |
|
| 225 |
print("No FactCheck result, falling back to live web analysis...")
|
| 226 |
return self.search_and_analyze_claim(claim)
|
| 227 |
+
|
| 228 |
class img_manipulation:
|
| 229 |
def __init__(self):
|
| 230 |
self.GEN_AI_IMAGE = pipeline("image-classification", model="umm-maybe/AI-image-detector", device=DEVICE)
|
|
|
|
| 240 |
print(f'AI image detection error: {e}')
|
| 241 |
return 0.0
|
| 242 |
|
| 243 |
+
def generated_image(self, img_pth, quality=90):
|
| 244 |
+
"""
|
| 245 |
+
Calculates the ELA score entirely in memory without saving any files.
|
| 246 |
+
"""
|
| 247 |
try:
|
| 248 |
with Image.open(img_pth) as orig_img:
|
| 249 |
orig_img = orig_img.convert('RGB')
|
| 250 |
+
|
| 251 |
+
# Create an in-memory buffer to hold the re-saved image
|
| 252 |
+
buffer = io.BytesIO()
|
| 253 |
+
orig_img.save(buffer, 'JPEG', quality=quality)
|
| 254 |
+
buffer.seek(0) # Rewind buffer to the beginning
|
| 255 |
+
|
| 256 |
+
with Image.open(buffer) as resaved_img:
|
| 257 |
+
# Calculate the difference between the original and re-saved image
|
| 258 |
ela_image = ImageChops.difference(orig_img, resaved_img)
|
| 259 |
+
|
| 260 |
ela_data = np.array(ela_image)
|
| 261 |
mean_intensity = ela_data.mean()
|
| 262 |
scaled_score = min(100, (mean_intensity / 25.0) * 100)
|
| 263 |
+
return scaled_score
|
| 264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
except Exception as e:
|
| 266 |
+
print(f'ELA calculation error: {e}')
|
| 267 |
+
return 0.0
|
| 268 |
|
| 269 |
def run_image_forensics(self, image_path):
|
| 270 |
ai_score = self.Gen_AI_IMG(image_path)
|
| 271 |
+
classic_score = self.generated_image(image_path)
|
| 272 |
+
|
| 273 |
+
# The return dictionary no longer includes 'ela_image_path'
|
| 274 |
return {
|
| 275 |
"ai_generated_score_percent": ai_score,
|
| 276 |
"classic_edit_score_percent": classic_score,
|
|
|
|
| 277 |
}
|
| 278 |
|
| 279 |
class OCR:
|