Sattfaceee123 commited on
Commit
230922e
·
verified ·
1 Parent(s): 903ade0

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +36 -38
index.html CHANGED
@@ -777,33 +777,30 @@ async function run(){
777
  try {
778
  const HF_API = "https://sattfaceee123-deepscan.hf.space";
779
 
780
- // Step 1: Upload image — Gradio 6 endpoint
781
  const blob = await fetch(currentImageData).then(r=>r.blob());
782
- const fd = new FormData();
783
- fd.append("files", blob, "image.jpg");
784
-
785
- const upRes = await fetch(`${HF_API}/upload`, {method:"POST", body:fd});
786
- if(!upRes.ok) throw new Error("Upload failed: "+upRes.status);
787
- const upJson = await upRes.json();
788
- // Gradio 6 returns array of objects with .path or array of strings
789
- let filePath;
790
- if(Array.isArray(upJson)){
791
- filePath = (typeof upJson[0] === 'object') ? upJson[0].path : upJson[0];
792
- } else {
793
- filePath = upJson.path || upJson;
794
- }
795
 
796
- // Step 2: Run prediction — Gradio 6 endpoint
 
 
 
 
 
 
 
 
 
 
797
  const predRes = await fetch(`${HF_API}/run/predict`, {
798
- method:"POST",
799
- headers:{"Content-Type":"application/json"},
800
  body: JSON.stringify({
801
  fn_index: 0,
802
- data: [{"path": filePath, "orig_name":"image.jpg", "mime_type":"image/jpeg", "size": blob.size}],
803
- event_data: null,
804
- session_hash: Math.random().toString(36).slice(2)
805
  })
806
  });
 
807
  if(!predRes.ok) throw new Error("Predict failed: "+predRes.status);
808
  const predJson = await predRes.json();
809
 
@@ -812,28 +809,29 @@ async function run(){
812
  document.getElementById("proc-bar").style.width="100%";
813
  st.textContent="Analysis complete ✦";
814
 
815
- // predJson.data[0] = label dict {"AI Generated": 0.87, "Real": 0.13}
816
- // predJson.data[1] = markdown verdict string
 
817
  const labelObj = predJson.data ? predJson.data[0] : {};
818
- const detailStr = predJson.data ? (predJson.data[1] || "") : "";
819
-
820
- let aiScore, realScore;
821
- if(labelObj && typeof labelObj === "object" && "confidences" in labelObj){
822
- // Gradio Label component format
823
- const confs = labelObj.confidences || [];
824
- const aiConf = confs.find(c=>c.label==="AI Generated");
825
- const realConf = confs.find(c=>c.label==="Real");
826
- aiScore = aiConf ? Math.round(aiConf.confidence*100) : 50;
827
- realScore = realConf ? Math.round(realConf.confidence*100) : 50;
828
  } else if(labelObj && "AI Generated" in labelObj){
829
  aiScore = Math.round((labelObj["AI Generated"]||0)*100);
830
  realScore = Math.round((labelObj["Real"]||0)*100);
831
  } else {
832
- // Fallback: parse from markdown
833
- const aiM = detailStr.match(/AI[^*]*\*\*([\d.]+)%\*\*/);
834
- const rlM = detailStr.match(/Real[^*]*\*\*([\d.]+)%\*\*/);
835
- aiScore = aiM ? parseFloat(aiM[1]) : (detailStr.includes("AI") ? 75 : 20);
836
- realScore = rlM ? parseFloat(rlM[1]) : Math.max(0, 100-aiScore);
837
  }
838
  const dfScore = Math.max(0, 100 - aiScore - realScore);
839
  setTimeout(()=>show(Math.round(aiScore), Math.round(realScore), dfScore), 500);
@@ -937,4 +935,4 @@ document.getElementById('btn-reset').addEventListener('click',()=>{
937
  res.style.display='none';pw.style.display='none';ip.src='';fi.value='';ba.style.display='none';
938
  });
939
  </script>
940
- </body></html>
 
777
  try {
778
  const HF_API = "https://sattfaceee123-deepscan.hf.space";
779
 
780
+ // Convert base64 image to blob
781
  const blob = await fetch(currentImageData).then(r=>r.blob());
 
 
 
 
 
 
 
 
 
 
 
 
 
782
 
783
+ // Gradio 6: convert image to base64 and send directly in data array
784
+ const toBase64 = b => new Promise((res,rej)=>{
785
+ const reader = new FileReader();
786
+ reader.onload = ()=>res(reader.result);
787
+ reader.onerror = rej;
788
+ reader.readAsDataURL(b);
789
+ });
790
+ const b64 = await toBase64(blob);
791
+
792
+ // Gradio 6 direct predict — send image as base64 data URL in data[]
793
+ const session = Math.random().toString(36).slice(2);
794
  const predRes = await fetch(`${HF_API}/run/predict`, {
795
+ method: "POST",
796
+ headers: {"Content-Type":"application/json"},
797
  body: JSON.stringify({
798
  fn_index: 0,
799
+ data: [b64],
800
+ session_hash: session
 
801
  })
802
  });
803
+
804
  if(!predRes.ok) throw new Error("Predict failed: "+predRes.status);
805
  const predJson = await predRes.json();
806
 
 
809
  document.getElementById("proc-bar").style.width="100%";
810
  st.textContent="Analysis complete ✦";
811
 
812
+ // Parse Gradio 6 Label output
813
+ // data[0] = {label: "AI Generated", confidences: [{label,confidence},...]}
814
+ // data[1] = markdown string
815
  const labelObj = predJson.data ? predJson.data[0] : {};
816
+ const detailStr = predJson.data ? String(predJson.data[1]||"") : "";
817
+
818
+ let aiScore = 50, realScore = 50;
819
+
820
+ if(labelObj && labelObj.confidences){
821
+ const confs = labelObj.confidences;
822
+ const aiC = confs.find(c=>c.label==="AI Generated");
823
+ const reC = confs.find(c=>c.label==="Real");
824
+ if(aiC) aiScore = Math.round(aiC.confidence*100);
825
+ if(reC) realScore = Math.round(reC.confidence*100);
826
  } else if(labelObj && "AI Generated" in labelObj){
827
  aiScore = Math.round((labelObj["AI Generated"]||0)*100);
828
  realScore = Math.round((labelObj["Real"]||0)*100);
829
  } else {
830
+ // last resort: parse markdown
831
+ const am = detailStr.match(/AI[^*]*\*\*([\d.]+)%\*\*/);
832
+ const rm = detailStr.match(/Real[^*]*\*\*([\d.]+)%\*\*/);
833
+ if(am) aiScore = parseFloat(am[1]);
834
+ if(rm) realScore = parseFloat(rm[1]);
835
  }
836
  const dfScore = Math.max(0, 100 - aiScore - realScore);
837
  setTimeout(()=>show(Math.round(aiScore), Math.round(realScore), dfScore), 500);
 
935
  res.style.display='none';pw.style.display='none';ip.src='';fi.value='';ba.style.display='none';
936
  });
937
  </script>
938
+ </body></html>