nivethitha1703 commited on
Commit
ba80160
·
verified ·
1 Parent(s): be1696b

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +14 -18
index.html CHANGED
@@ -32,8 +32,8 @@
32
  async function generateCommit() {
33
  const MODEL_ID = "nivethitha1703/developer-git-commit-agent";
34
 
35
- // FIX: Safely reads the hidden token from Hugging Face infrastructure without revealing it in your code
36
- const HF_TOKEN = window.huggingface?.variables?.HF_TOKEN;
37
 
38
  const diffText = document.getElementById("gitDiff").value;
39
  const outputBox = document.getElementById("outputBox");
@@ -44,12 +44,6 @@ async function generateCommit() {
44
  return;
45
  }
46
 
47
- if (!HF_TOKEN) {
48
- outputBox.innerText = "Error: HF_TOKEN secret not found in Space Settings. Please add it first.";
49
- outputBox.style.display = "block";
50
- return;
51
- }
52
-
53
  outputBox.style.display = "none";
54
  loadingStatus.style.display = "block";
55
 
@@ -59,7 +53,7 @@ async function generateCommit() {
59
  const response = await fetch(`https://huggingface.co{MODEL_ID}`, {
60
  method: "POST",
61
  headers: {
62
- "Authorization": `Bearer ${HF_TOKEN}`,
63
  "Content-Type": "application/json"
64
  },
65
  body: JSON.stringify({
@@ -71,24 +65,26 @@ async function generateCommit() {
71
  const data = await response.json();
72
  loadingStatus.style.display = "none";
73
 
 
 
 
 
 
 
 
74
  if (data.error) {
75
  outputBox.innerText = "API Error: " + data.error;
76
- } else if (Array.isArray(data) && data[0]?.generated_text) {
77
- let result = data[0].generated_text;
78
- result = result.replace(prompt, "").trim();
79
- outputBox.innerText = result;
80
- } else if (data.generated_text) {
81
- let result = data.generated_text;
82
  result = result.replace(prompt, "").trim();
83
  outputBox.innerText = result;
84
- } else {
85
- outputBox.innerText = JSON.stringify(data);
86
  }
87
  outputBox.style.display = "block";
88
 
89
  } catch (error) {
90
  loadingStatus.style.display = "none";
91
- outputBox.innerText = "Error calling Inference API: " + error.message;
92
  outputBox.style.display = "block";
93
  }
94
  }
 
32
  async function generateCommit() {
33
  const MODEL_ID = "nivethitha1703/developer-git-commit-agent";
34
 
35
+ // Safely reads the public variable without hardcoding the token string in text
36
+ const HF_TOKEN = window.huggingface?.variables?.HF_TOKEN || "";
37
 
38
  const diffText = document.getElementById("gitDiff").value;
39
  const outputBox = document.getElementById("outputBox");
 
44
  return;
45
  }
46
 
 
 
 
 
 
 
47
  outputBox.style.display = "none";
48
  loadingStatus.style.display = "block";
49
 
 
53
  const response = await fetch(`https://huggingface.co{MODEL_ID}`, {
54
  method: "POST",
55
  headers: {
56
+ "Authorization": HF_TOKEN ? `Bearer ${HF_TOKEN}` : "",
57
  "Content-Type": "application/json"
58
  },
59
  body: JSON.stringify({
 
65
  const data = await response.json();
66
  loadingStatus.style.display = "none";
67
 
68
+ if (data.error && data.error.includes("loading")) {
69
+ outputBox.innerText = "⏳ Model is currently loading on Hugging Face servers. Retrying automatically in 10 seconds...";
70
+ outputBox.style.display = "block";
71
+ setTimeout(generateCommit, 10000);
72
+ return;
73
+ }
74
+
75
  if (data.error) {
76
  outputBox.innerText = "API Error: " + data.error;
77
+ } else {
78
+ // Support both array and object response structures from the inference API
79
+ let result = Array.isArray(data) ? data[0].generated_text : data.generated_text;
 
 
 
80
  result = result.replace(prompt, "").trim();
81
  outputBox.innerText = result;
 
 
82
  }
83
  outputBox.style.display = "block";
84
 
85
  } catch (error) {
86
  loadingStatus.style.display = "none";
87
+ outputBox.innerText = "Fetch Error: " + error.message;
88
  outputBox.style.display = "block";
89
  }
90
  }