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|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Any
|
| 3 |
+
|
| 4 |
+
from google import genai
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# ============================================================
|
| 9 |
+
# CONFIGURATION
|
| 10 |
+
# ============================================================
|
| 11 |
+
|
| 12 |
+
# Model dapat diganti melalui environment variable HF Space
|
| 13 |
+
GEMINI_MODEL = os.getenv(
|
| 14 |
+
"GEMINI_MODEL",
|
| 15 |
+
"gemini-3.5-flash"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# ============================================================
|
| 20 |
+
# STRUCTURED OUTPUT SCHEMA
|
| 21 |
+
# ============================================================
|
| 22 |
+
|
| 23 |
+
class AIAnalysisResult(BaseModel):
|
| 24 |
+
summary: str = Field(
|
| 25 |
+
description=(
|
| 26 |
+
"Ringkasan singkat hasil deteksi deepfake audio "
|
| 27 |
+
"dalam Bahasa Indonesia."
|
| 28 |
+
)
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
recommendation: list[str] = Field(
|
| 32 |
+
description=(
|
| 33 |
+
"Daftar tindakan lanjutan yang praktis dan relevan."
|
| 34 |
+
)
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
disclaimer: str = Field(
|
| 38 |
+
description=(
|
| 39 |
+
"Peringatan bahwa hasil deteksi bukan bukti forensik final."
|
| 40 |
+
)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ============================================================
|
| 45 |
+
# GEMINI CLIENT
|
| 46 |
+
# ============================================================
|
| 47 |
+
|
| 48 |
+
def get_gemini_client() -> genai.Client:
|
| 49 |
+
"""
|
| 50 |
+
Membuat Gemini client.
|
| 51 |
+
|
| 52 |
+
API key dibaca otomatis dari environment variable:
|
| 53 |
+
GEMINI_API_KEY
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
api_key = os.getenv(
|
| 57 |
+
"GEMINI_API_KEY"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if not api_key:
|
| 61 |
+
raise RuntimeError(
|
| 62 |
+
"GEMINI_API_KEY belum dikonfigurasi."
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
return genai.Client(
|
| 66 |
+
api_key=api_key
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# ============================================================
|
| 71 |
+
# GENERATE AI EXPLANATION
|
| 72 |
+
# ============================================================
|
| 73 |
+
|
| 74 |
+
def generate_detection_analysis(
|
| 75 |
+
detection_result: dict[str, Any]
|
| 76 |
+
) -> dict[str, Any]:
|
| 77 |
+
"""
|
| 78 |
+
Membuat penjelasan hasil deteksi menggunakan Gemini API.
|
| 79 |
+
|
| 80 |
+
Gemini tidak menentukan label real atau fake.
|
| 81 |
+
Gemini hanya menjelaskan output model TensorFlow.
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
client = get_gemini_client()
|
| 85 |
+
|
| 86 |
+
prediction = detection_result["prediction"]
|
| 87 |
+
threshold = detection_result["threshold"]
|
| 88 |
+
total_clips = detection_result["total_clips"]
|
| 89 |
+
real_clips = detection_result["real_clips"]
|
| 90 |
+
fake_clips = detection_result["fake_clips"]
|
| 91 |
+
|
| 92 |
+
average_probability_real = detection_result[
|
| 93 |
+
"average_probability_real"
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
average_probability_fake = detection_result[
|
| 97 |
+
"average_probability_fake"
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
prompt = f"""
|
| 101 |
+
Anda adalah AI Analysis Assistant untuk aplikasi pendeteksi
|
| 102 |
+
deepfake audio.
|
| 103 |
+
|
| 104 |
+
Buat penjelasan yang mudah dipahami oleh pengguna umum dalam
|
| 105 |
+
Bahasa Indonesia.
|
| 106 |
+
|
| 107 |
+
PENTING:
|
| 108 |
+
- Jangan mengubah hasil klasifikasi dari model TensorFlow.
|
| 109 |
+
- Jangan menyatakan hasil sebagai bukti forensik final.
|
| 110 |
+
- Jangan membuat klaim berlebihan.
|
| 111 |
+
- Berikan rekomendasi praktis.
|
| 112 |
+
- Gunakan kalimat yang ringkas dan jelas.
|
| 113 |
+
|
| 114 |
+
Berikut hasil deteksi dari model TensorFlow:
|
| 115 |
+
|
| 116 |
+
prediction = {prediction}
|
| 117 |
+
threshold = {threshold}
|
| 118 |
+
total_clips = {total_clips}
|
| 119 |
+
real_clips = {real_clips}
|
| 120 |
+
fake_clips = {fake_clips}
|
| 121 |
+
average_probability_real = {average_probability_real}
|
| 122 |
+
average_probability_fake = {average_probability_fake}
|
| 123 |
+
|
| 124 |
+
Buat:
|
| 125 |
+
1. Ringkasan hasil analisis.
|
| 126 |
+
2. Rekomendasi tindakan lanjutan.
|
| 127 |
+
3. Disclaimer bahwa hasil ini merupakan indikasi awal dan
|
| 128 |
+
bukan bukti forensik final.
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
response = client.models.generate_content(
|
| 132 |
+
model=GEMINI_MODEL,
|
| 133 |
+
contents=prompt,
|
| 134 |
+
config={
|
| 135 |
+
"response_format": {
|
| 136 |
+
"text": {
|
| 137 |
+
"mime_type": "application/json",
|
| 138 |
+
"schema": (
|
| 139 |
+
AIAnalysisResult
|
| 140 |
+
.model_json_schema()
|
| 141 |
+
)
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
if not response.text:
|
| 148 |
+
raise RuntimeError(
|
| 149 |
+
"Gemini API tidak mengembalikan respons."
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
parsed_result = (
|
| 153 |
+
AIAnalysisResult
|
| 154 |
+
.model_validate_json(
|
| 155 |
+
response.text
|
| 156 |
+
)
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
return parsed_result.model_dump()
|