MAS-AI-0000 commited on
Commit
2bfc7cd
·
verified ·
1 Parent(s): cb736f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -1,13 +1,14 @@
1
  from fastapi import FastAPI, File, UploadFile, Body
2
- from fastapi.responses import RedirectResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from PIL import Image
5
  import io
6
  import numpy as np
7
  from structure import TextPredictionRequest, PredictionResponse
8
- from detector import detect_embedding
9
  from text_embedder import get_text_embedding
10
  from image_embedder import get_image_embedding
 
11
 
12
  origins=[
13
  "http://localhost:5173",
@@ -36,6 +37,26 @@ async def root():
36
  # Redirect to the automatic Swagger UI provided by FastAPI
37
  return RedirectResponse(url="/docs")
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  @app.post(
40
  "/predict/image",
41
  response_model=PredictionResponse,
@@ -49,12 +70,12 @@ async def predict(image: UploadFile = File(...)):
49
  image_data = await image.read()
50
  pil_img = Image.open(io.BytesIO(image_data)).convert("RGB")
51
  emb = get_image_embedding(pil_img)
52
- prediction = detect_embedding(emb)
53
  print(f"Image prediction: {prediction['predicted_class']} with confidence {prediction['confidence']:.4f}")
54
  return PredictionResponse(predicted_class=prediction["predicted_class"], confidence=prediction["confidence"])
55
  except Exception as e:
56
  print(f"Error in image prediction: {e}")
57
- return PredictionResponse(predicted_class=0, confidence=0)
58
  @app.post(
59
  "/predict/text",
60
  response_model=PredictionResponse,
@@ -64,7 +85,8 @@ async def predict(image: UploadFile = File(...)):
64
  async def predict_text_endpoint(payload: TextPredictionRequest = Body(...)):
65
  """Accept a text string and return a prediction of whether it's human or AI-generated."""
66
  try:
67
- emb = get_text_embedding(payload.text)
 
68
  result = detect_embedding(emb)
69
 
70
  return PredictionResponse(
@@ -74,4 +96,4 @@ async def predict_text_endpoint(payload: TextPredictionRequest = Body(...)):
74
  except Exception as e:
75
  # Return a fallback response in case of error
76
  print(f"Error in text prediction: {e}")
77
- return PredictionResponse(predicted_class=0, confidence=0)
 
1
  from fastapi import FastAPI, File, UploadFile, Body
2
+ from fastapi.responses import RedirectResponse, StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from PIL import Image
5
  import io
6
  import numpy as np
7
  from structure import TextPredictionRequest, PredictionResponse
8
+ from detector import detect_embedding, _active_layer
9
  from text_embedder import get_text_embedding
10
  from image_embedder import get_image_embedding
11
+ from kuwahara import apply_kuwahara
12
 
13
  origins=[
14
  "http://localhost:5173",
 
37
  # Redirect to the automatic Swagger UI provided by FastAPI
38
  return RedirectResponse(url="/docs")
39
 
40
+ @app.post(
41
+ "/debug/kuwahara",
42
+ summary="Debug: Get raw Kuwahara output",
43
+ description="Upload an image to see exactly how the Kuwahara filter transforms it in memory.",
44
+ )
45
+ async def debug_kuwahara(image: UploadFile = File(...)):
46
+ """Accept an image upload and return the Kuwahara processed image directly."""
47
+ try:
48
+ image_data = await image.read()
49
+ pil_img = Image.open(io.BytesIO(image_data)).convert("RGB")
50
+ processed_img = apply_kuwahara(pil_img)
51
+ # Save to memory buffer and stream
52
+ buf = io.BytesIO()
53
+ processed_img.save(buf, format="PNG")
54
+ buf.seek(0)
55
+ return StreamingResponse(buf, media_type="image/png")
56
+ except Exception as e:
57
+ print(f"Error in debug kuwahara: {e}")
58
+ return {"error": str(e)}
59
+
60
  @app.post(
61
  "/predict/image",
62
  response_model=PredictionResponse,
 
70
  image_data = await image.read()
71
  pil_img = Image.open(io.BytesIO(image_data)).convert("RGB")
72
  emb = get_image_embedding(pil_img)
73
+ prediction = detect_embedding(emb, threshold=0.5)
74
  print(f"Image prediction: {prediction['predicted_class']} with confidence {prediction['confidence']:.4f}")
75
  return PredictionResponse(predicted_class=prediction["predicted_class"], confidence=prediction["confidence"])
76
  except Exception as e:
77
  print(f"Error in image prediction: {e}")
78
+ return PredictionResponse(predicted_class=0, confidence=0.0)
79
  @app.post(
80
  "/predict/text",
81
  response_model=PredictionResponse,
 
85
  async def predict_text_endpoint(payload: TextPredictionRequest = Body(...)):
86
  """Accept a text string and return a prediction of whether it's human or AI-generated."""
87
  try:
88
+ layer = _active_layer if _active_layer is not None else -1
89
+ emb = get_text_embedding(payload.text, layer=layer)
90
  result = detect_embedding(emb)
91
 
92
  return PredictionResponse(
 
96
  except Exception as e:
97
  # Return a fallback response in case of error
98
  print(f"Error in text prediction: {e}")
99
+ return PredictionResponse(predicted_class=0, confidence=0.0)