Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,105 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
flagging_mode="never",
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
if __name__ == "__main__":
|
| 110 |
-
demo.launch()
|
|
|
|
| 1 |
+
/// <reference types="vite/client" />
|
| 2 |
+
|
| 3 |
+
import { client } from "@gradio/client";
|
| 4 |
+
|
| 5 |
+
export const HF_SPACE_URL = "https://thompson001-crack-api.hf.space/";
|
| 6 |
+
|
| 7 |
+
export interface InferenceResult {
|
| 8 |
+
hasCrack: boolean;
|
| 9 |
+
confidence: number;
|
| 10 |
+
label: string;
|
| 11 |
+
details: string;
|
| 12 |
+
isCustomModel: boolean;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// Gradio Result ๊ตฌ์กฐ
|
| 16 |
+
interface GradioEvent {
|
| 17 |
+
data: any[];
|
| 18 |
+
[key: string]: any;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
interface HFJsonItem {
|
| 22 |
+
label?: string;
|
| 23 |
+
confidence?: number;
|
| 24 |
+
message?: string;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
interface HFJsonPayload {
|
| 28 |
+
data: HFJsonItem[];
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// -------------------------------------------------------
|
| 32 |
+
// HF Space ํธ์ถ
|
| 33 |
+
// -------------------------------------------------------
|
| 34 |
+
export async function predictCrack(file: File): Promise<InferenceResult> {
|
| 35 |
+
console.log("๐ต Connecting to HF Spaceโฆ");
|
| 36 |
+
|
| 37 |
+
let app;
|
| 38 |
+
try {
|
| 39 |
+
app = await client(HF_SPACE_URL);
|
| 40 |
+
} catch (err) {
|
| 41 |
+
console.error("โ HF Space ์ฐ๊ฒฐ ์คํจ:", err);
|
| 42 |
+
throw new Error("HF Space ์ฐ๊ฒฐ ์คํจ");
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
console.log("๐ต Sending image to /predictโฆ");
|
| 46 |
+
|
| 47 |
+
let rawResult: GradioEvent;
|
| 48 |
+
try {
|
| 49 |
+
rawResult = (await app.predict("/predict", {
|
| 50 |
+
img: file, // ๋ฐ๋์ img
|
| 51 |
+
})) as GradioEvent;
|
| 52 |
+
} catch (err: any) {
|
| 53 |
+
console.error("โ /predict ํธ์ถ ์คํจ:", err);
|
| 54 |
+
throw new Error("HF Space ์์ธก ํธ์ถ ์คํจ");
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
console.log("๐ต Raw Response:", rawResult);
|
| 58 |
+
|
| 59 |
+
const jsonPayload = rawResult?.data?.[0] as HFJsonPayload;
|
| 60 |
+
|
| 61 |
+
console.log("๐ต JSON Payload:", jsonPayload);
|
| 62 |
+
|
| 63 |
+
// JSON ์๋ ๊ฒฝ์ฐ โ ์ ์ ์ทจ๊ธ
|
| 64 |
+
if (!jsonPayload?.data || jsonPayload.data.length === 0) {
|
| 65 |
+
return {
|
| 66 |
+
hasCrack: false,
|
| 67 |
+
confidence: 1,
|
| 68 |
+
label: "์ ์",
|
| 69 |
+
details: "",
|
| 70 |
+
isCustomModel: true,
|
| 71 |
+
};
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
const first: HFJsonItem = jsonPayload.data[0];
|
| 75 |
+
|
| 76 |
+
const rawLabel = typeof first.label === "string" ? first.label : "normal";
|
| 77 |
+
const confidence =
|
| 78 |
+
typeof first.confidence === "number" ? first.confidence : 1;
|
| 79 |
+
|
| 80 |
+
const normalized = rawLabel.replace(/\s+/g, "").toLowerCase();
|
| 81 |
+
|
| 82 |
+
// ์๋ฌ ์ฒ๋ฆฌ
|
| 83 |
+
if (normalized === "error") {
|
| 84 |
+
throw new Error(first.message || "๋ชจ๋ธ ๋ด๋ถ ์ค๋ฅ");
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// ๊ท ์ด ํ๋จ: crack, damaged ๋ฑ ํฌํจ
|
| 88 |
+
const isCrack =
|
| 89 |
+
normalized.includes("crack") ||
|
| 90 |
+
normalized.includes("positive") ||
|
| 91 |
+
normalized.includes("damaged");
|
| 92 |
+
|
| 93 |
+
// ๐ฅ ๋๊ฐ ์ํ ๋ก์ง: ์ ์์ผ ๋๋ % ์จ๊น
|
| 94 |
+
const details = isCrack
|
| 95 |
+
? `๊ท ์ด ํ๋ฅ ${(confidence * 100).toFixed(1)}%`
|
| 96 |
+
: "";
|
| 97 |
+
|
| 98 |
+
return {
|
| 99 |
+
hasCrack: isCrack,
|
| 100 |
+
confidence,
|
| 101 |
+
label: isCrack ? "๊ท ์ด ๊ฐ์ง" : "์ ์",
|
| 102 |
+
details,
|
| 103 |
+
isCustomModel: true,
|
| 104 |
+
};
|
| 105 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|