Syahhh01 commited on
Commit
c9fd5f9
·
verified ·
1 Parent(s): 3203265

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -1
app.py CHANGED
@@ -2,7 +2,13 @@ from contextlib import asynccontextmanager
2
  from pathlib import Path
3
  from tempfile import NamedTemporaryFile
4
  from typing import Annotated
 
5
 
 
 
 
 
 
6
  import tensorflow as tf
7
  from fastapi import (
8
  FastAPI,
@@ -92,6 +98,43 @@ app = FastAPI(
92
  )
93
 
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # ============================================================
96
  # ROUTES
97
  # ============================================================
@@ -230,4 +273,60 @@ async def predict(
230
  temp_path is not None
231
  and temp_path.exists()
232
  ):
233
- temp_path.unlink()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from pathlib import Path
3
  from tempfile import NamedTemporaryFile
4
  from typing import Annotated
5
+ from typing import Any
6
 
7
+ from pydantic import BaseModel, Field
8
+
9
+ from genai_service import (
10
+ generate_detection_analysis
11
+ )
12
  import tensorflow as tf
13
  from fastapi import (
14
  FastAPI,
 
98
  )
99
 
100
 
101
+ # ============================================================
102
+ # GENERATIVE AI REQUEST SCHEMA
103
+ # ============================================================
104
+
105
+ class DetectionAnalysisRequest(BaseModel):
106
+ prediction: str = Field(
107
+ pattern="^(real|fake)$"
108
+ )
109
+
110
+ threshold: float = Field(
111
+ ge=0.0,
112
+ le=1.0
113
+ )
114
+
115
+ total_clips: int = Field(
116
+ ge=1
117
+ )
118
+
119
+ real_clips: int = Field(
120
+ ge=0
121
+ )
122
+
123
+ fake_clips: int = Field(
124
+ ge=0
125
+ )
126
+
127
+ average_probability_real: float = Field(
128
+ ge=0.0,
129
+ le=1.0
130
+ )
131
+
132
+ average_probability_fake: float = Field(
133
+ ge=0.0,
134
+ le=1.0
135
+ )
136
+
137
+
138
  # ============================================================
139
  # ROUTES
140
  # ============================================================
 
273
  temp_path is not None
274
  and temp_path.exists()
275
  ):
276
+ temp_path.unlink()
277
+
278
+
279
+ # ============================================================
280
+ # GENERATIVE AI ANALYSIS ENDPOINT
281
+ # ============================================================
282
+
283
+ @app.post("/generate-analysis")
284
+ def generate_analysis(
285
+ request: DetectionAnalysisRequest
286
+ ):
287
+ """
288
+ Membuat penjelasan hasil prediksi menggunakan Gemini API.
289
+
290
+ Endpoint ini merupakan fitur sekunder.
291
+ Label prediksi tetap berasal dari model TensorFlow.
292
+ """
293
+
294
+ if (
295
+ request.real_clips
296
+ + request.fake_clips
297
+ != request.total_clips
298
+ ):
299
+ raise HTTPException(
300
+ status_code=400,
301
+ detail=(
302
+ "Jumlah real_clips dan fake_clips "
303
+ "harus sama dengan total_clips."
304
+ )
305
+ )
306
+
307
+ try:
308
+ analysis = generate_detection_analysis(
309
+ detection_result=(
310
+ request.model_dump()
311
+ )
312
+ )
313
+
314
+ return {
315
+ "prediction": request.prediction,
316
+ "analysis": analysis
317
+ }
318
+
319
+ except RuntimeError as error:
320
+ raise HTTPException(
321
+ status_code=503,
322
+ detail=str(error)
323
+ ) from error
324
+
325
+ except Exception as error:
326
+ raise HTTPException(
327
+ status_code=500,
328
+ detail=(
329
+ "Gagal membuat analisis AI: "
330
+ f"{str(error)}"
331
+ )
332
+ ) from error