Andercar commited on
Commit
15c813b
·
verified ·
1 Parent(s): 2529a71

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +60 -60
main.py CHANGED
@@ -1,60 +1,60 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from PIL import Image
4
- import io
5
- from transformers import pipeline
6
-
7
- app = FastAPI()
8
-
9
- # Allow CORS
10
- app.add_middleware(
11
- CORSMiddleware,
12
- allow_origins=["*"],
13
- allow_credentials=True,
14
- allow_methods=["*"],
15
- allow_headers=["*"],
16
- )
17
-
18
- # Load AI Detection Model
19
- # We use a pre-trained model from Hugging Face
20
- # 'umm-maybe/AI-image-detector' is a good general purpose detector
21
- print("Loading AI Model...")
22
- classifier = pipeline("image-classification", model="umm-maybe/AI-image-detector")
23
- print("Model Loaded!")
24
-
25
- @app.post("/analyze")
26
- async def analyze_media(file: UploadFile = File(...)):
27
- try:
28
- contents = await file.read()
29
- image = Image.open(io.BytesIO(contents))
30
-
31
- # Run Inference
32
- results = classifier(image)
33
- # results is a list like [{'label': 'artificial', 'score': 0.99}, {'label': 'human', 'score': 0.01}]
34
-
35
- # Find the 'artificial' or 'AI' score
36
- ai_score = 0.0
37
- for r in results:
38
- label = r['label'].lower()
39
- if 'artificial' in label or 'ai' in label:
40
- ai_score = r['score']
41
- break
42
- if 'human' in label or 'real' in label:
43
- # If we found human score, AI score is 1 - human
44
- ai_score = 1.0 - r['score']
45
-
46
- is_ai = ai_score > 0.5
47
-
48
- return {
49
- "filename": file.filename,
50
- "is_ai": is_ai,
51
- "confidence": round(ai_score * 100, 2), # Return 0-100
52
- "details": results
53
- }
54
- except Exception as e:
55
- print(f"Error: {e}")
56
- raise HTTPException(status_code=500, detail=str(e))
57
-
58
- @app.get("/")
59
- def read_root():
60
- return {"status": "AI Detector Neural Network is Running"}
 
1
+ from fastapi import FastAPI, UploadFile, File, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from PIL import Image
4
+ import io
5
+ from transformers import pipeline
6
+
7
+ app = FastAPI()
8
+
9
+ # Allow CORS
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ # Load AI Detection Model
19
+ # We use a pre-trained model from Hugging Face
20
+ # 'Organika/sdxl-detector' is specialized for Stable Diffusion detection
21
+ print("Loading AI Model...")
22
+ classifier = pipeline("image-classification", model="Organika/sdxl-detector")
23
+ print("Model Loaded!")
24
+
25
+ @app.post("/analyze")
26
+ async def analyze_media(file: UploadFile = File(...)):
27
+ try:
28
+ contents = await file.read()
29
+ image = Image.open(io.BytesIO(contents))
30
+
31
+ # Run Inference
32
+ results = classifier(image)
33
+ # results is a list like [{'label': 'artificial', 'score': 0.99}, {'label': 'human', 'score': 0.01}]
34
+
35
+ # Find the 'artificial' or 'AI' score
36
+ ai_score = 0.0
37
+ for r in results:
38
+ label = r['label'].lower()
39
+ if 'artificial' in label or 'ai' in label:
40
+ ai_score = r['score']
41
+ break
42
+ if 'human' in label or 'real' in label:
43
+ # If we found human score, AI score is 1 - human
44
+ ai_score = 1.0 - r['score']
45
+
46
+ is_ai = ai_score > 0.5
47
+
48
+ return {
49
+ "filename": file.filename,
50
+ "is_ai": is_ai,
51
+ "confidence": round(ai_score * 100, 2), # Return 0-100
52
+ "details": results
53
+ }
54
+ except Exception as e:
55
+ print(f"Error: {e}")
56
+ raise HTTPException(status_code=500, detail=str(e))
57
+
58
+ @app.get("/")
59
+ def read_root():
60
+ return {"status": "AI Detector Neural Network is Running"}