FlashCode-Lab commited on
Commit
1f52d20
·
verified ·
1 Parent(s): 21ff3d0

Create app.js

Browse files
Files changed (1) hide show
  1. app.js +164 -0
app.js ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const STORAGE_KEY = "my-omni-ai-config";
2
+
3
+ const ids = ["hfToken", "chatModel", "codeModel", "writerModel", "videoModel", "faceModel"];
4
+
5
+ const outputs = {
6
+ chat: document.getElementById("chatOutput"),
7
+ code: document.getElementById("codeOutput"),
8
+ writing: document.getElementById("writingOutput"),
9
+ video: document.getElementById("videoOutput"),
10
+ faceswap: document.getElementById("faceOutput")
11
+ };
12
+
13
+ function loadConfig() {
14
+ const cached = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
15
+ ids.forEach((id) => {
16
+ if (cached[id]) document.getElementById(id).value = cached[id];
17
+ });
18
+ }
19
+
20
+ function getConfig() {
21
+ return Object.fromEntries(ids.map((id) => [id, document.getElementById(id).value.trim()]));
22
+ }
23
+
24
+ function saveConfig() {
25
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(getConfig()));
26
+ alert("配置已保存到浏览器");
27
+ }
28
+
29
+ async function hfTextInference(model, prompt, token) {
30
+ const res = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
31
+ method: "POST",
32
+ headers: {
33
+ Authorization: `Bearer ${token}`,
34
+ "Content-Type": "application/json"
35
+ },
36
+ body: JSON.stringify({
37
+ inputs: prompt,
38
+ parameters: {
39
+ max_new_tokens: 1024,
40
+ temperature: 0.7,
41
+ return_full_text: false
42
+ }
43
+ })
44
+ });
45
+
46
+ if (!res.ok) {
47
+ const text = await res.text();
48
+ throw new Error(`模型调用失败:${res.status} ${text}`);
49
+ }
50
+
51
+ const data = await res.json();
52
+ if (Array.isArray(data) && data[0]?.generated_text) return data[0].generated_text;
53
+ if (data?.generated_text) return data.generated_text;
54
+ return JSON.stringify(data, null, 2);
55
+ }
56
+
57
+ async function hfGenericTask(model, prompt, token) {
58
+ const res = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
59
+ method: "POST",
60
+ headers: {
61
+ Authorization: `Bearer ${token}`,
62
+ "Content-Type": "application/json"
63
+ },
64
+ body: JSON.stringify({ inputs: prompt })
65
+ });
66
+ const text = await res.text();
67
+ if (!res.ok) throw new Error(`任务失败:${res.status} ${text}`);
68
+ return text;
69
+ }
70
+
71
+ async function runAction(kind) {
72
+ const config = getConfig();
73
+ const token = config.hfToken;
74
+
75
+ if (!token) {
76
+ outputs[kind].textContent = "请先填写 Hugging Face Token";
77
+ return;
78
+ }
79
+
80
+ const promptByKind = {
81
+ chat: document.getElementById("chatPrompt").value,
82
+ code: document.getElementById("codePrompt").value,
83
+ writing: document.getElementById("writingPrompt").value,
84
+ video: document.getElementById("videoPrompt").value,
85
+ faceswap: document.getElementById("facePrompt").value
86
+ };
87
+
88
+ const modelByKind = {
89
+ chat: config.chatModel,
90
+ code: config.codeModel,
91
+ writing: config.writerModel,
92
+ video: config.videoModel,
93
+ faceswap: config.faceModel
94
+ };
95
+
96
+ outputs[kind].textContent = "正在调用模型,请稍候...";
97
+
98
+ try {
99
+ if (["chat", "code", "writing"].includes(kind)) {
100
+ const answer = await hfTextInference(modelByKind[kind], promptByKind[kind], token);
101
+ outputs[kind].textContent = answer;
102
+ } else {
103
+ const answer = await hfGenericTask(modelByKind[kind], promptByKind[kind], token);
104
+ outputs[kind].textContent = `已提交任务(原始响应):\n${answer}`;
105
+ }
106
+ } catch (error) {
107
+ outputs[kind].textContent = String(error);
108
+ }
109
+ }
110
+
111
+ function bindTabs() {
112
+ const tabButtons = document.querySelectorAll(".tab");
113
+ const panels = document.querySelectorAll(".tab-panel");
114
+
115
+ tabButtons.forEach((btn) => {
116
+ btn.addEventListener("click", () => {
117
+ tabButtons.forEach((x) => x.classList.remove("active"));
118
+ panels.forEach((x) => x.classList.remove("active"));
119
+ btn.classList.add("active");
120
+ document.getElementById(btn.dataset.tab).classList.add("active");
121
+ });
122
+ });
123
+ }
124
+
125
+ function bindActions() {
126
+ document.querySelectorAll("button[data-action]").forEach((btn) => {
127
+ btn.addEventListener("click", () => runAction(btn.dataset.action));
128
+ });
129
+
130
+ document.getElementById("saveConfigBtn").addEventListener("click", saveConfig);
131
+ }
132
+
133
+ loadConfig();
134
+ bindTabs();
135
+ bindActions();
136
+ bindDeployCommandHelper();
137
+
138
+
139
+ function bindDeployCommandHelper() {
140
+ const output = document.getElementById("deployCmdOutput");
141
+ const btn = document.getElementById("copyDeployCmdBtn");
142
+ if (!btn || !output) return;
143
+
144
+ btn.addEventListener("click", async () => {
145
+ const spaceName = document.getElementById("spaceName").value.trim();
146
+ if (!spaceName) {
147
+ output.textContent = "请先填写 Space 名称";
148
+ return;
149
+ }
150
+
151
+ const sdk = document.getElementById("spaceSdk")?.value || "static";
152
+ const privateFlag = document.getElementById("spacePrivate")?.checked ? " --private" : "";
153
+
154
+ const cmd = `HF_TOKEN=你的hf_token python scripts/deploy_to_hf_space.py --space ${spaceName} --sdk ${sdk}${privateFlag}`;
155
+ output.textContent = cmd;
156
+
157
+ try {
158
+ await navigator.clipboard.writeText(cmd);
159
+ output.textContent += "\n\n✅ 命令已复制,粘贴到终端执行���可一键发布。";
160
+ } catch {
161
+ output.textContent += "\n\n⚠️ 自动复制失败,请手动复制上面的命令。";
162
+ }
163
+ });
164
+ }