File size: 3,830 Bytes
821b0e4
 
 
 
 
c72cb42
 
821b0e4
 
477faa1
 
821b0e4
 
 
8781c4d
 
 
 
821b0e4
 
 
 
 
 
 
 
a7a3116
 
821b0e4
 
a7a3116
 
 
 
 
9bb466d
a7a3116
 
821b0e4
8781c4d
a7a3116
 
821b0e4
 
 
 
 
 
 
 
149db23
821b0e4
 
 
 
5da8e77
 
 
 
821b0e4
 
 
 
a7a3116
821b0e4
 
 
a7a3116
821b0e4
 
a7a3116
821b0e4
 
 
 
 
 
477faa1
c72cb42
693f001
c72cb42
 
 
c22f19c
c72cb42
8781c4d
c22f19c
 
 
 
693f001
c72cb42
 
 
 
 
 
 
 
da8cca5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
821b0e4
477faa1
821b0e4
 
149db23
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// server.js
import express from "express";
import { Client } from "@gradio/client";
import dotenv from "dotenv";
import fetch from "node-fetch";
import multer from "multer";
const upload = multer();

dotenv.config();

const app = express();
const PORT = process.env.PORT || 7860;
const HF_TOKEN = process.env.HF_TOKEN;

// Reuse a single Gradio client so state (like dropdown choices)
// is preserved across related function calls.
const clientPromise = Client.connect("pockit-cloud/main", { hf_token: HF_TOKEN });

if (!HF_TOKEN) {
  console.error("HF_TOKEN is not set in environment variables!");
  process.exit(1);
}

app.use(express.json({ limit: "10mb" }));

app.post("/api/proxy", async (req, res) => {
  let { fn, args } = req.body;
  
  if (!fn) return res.status(400).json({ error: "Missing 'fn' in request body" });

  // THE FIX: Scrub the 'args' object clean before passing it to Gradio.
  // If your frontend accidentally sent 'read_token' in the payload, this deletes it.
  if (args && typeof args === 'object' && !Array.isArray(args)) {
    delete args.read_token;
    delete args.hf_token;
    delete args.write_token;
  }

  try {
    const client = await clientPromise;
    
    // Now 'args' only contains the actual model inputs
    const result = await client.predict(fn, args);
    res.json({ data: result.data });
  } catch (err) {
    console.error("Proxy error:", err);
    res.status(500).json({ error: "Proxy call failed", details: err.message });
  }
});

// Download endpoint
app.get("/api/download", async (req, res) => {
  const { file } = req.query;
  if (!file) return res.status(400).json({ error: "Missing 'file' query param" });

  const url =
    typeof file === "string" && file.startsWith("http")
      ? file
      : `https://huggingface.co/datasets/${file}`;
  try {
    const response = await fetch(url, {
      headers: { Authorization: `Bearer ${HF_TOKEN}` },
    });
    
    if (!response.ok) {
      return res.status(response.status).json({ error: "File fetch failed" });
    }
    
    res.setHeader("Content-Disposition", `attachment; filename=\"${file.split("/").pop()}\"`);
    res.setHeader("Content-Type", response.headers.get("content-type") || "application/octet-stream");
    
    response.body.pipe(res);
  } catch (err) {
    console.error("Download error:", err);
    res.status(500).json({ error: "Download failed", details: err.message });
  }
});

app.post("/api/upload", upload.single("file"), async (req, res) => {
  const { user_id, password } = req.body;
  if (!req.file || !user_id || !password) {
    return res.status(400).json({ error: "Missing file, user_id, or password" });
  }
  const custom_name = req.body.custom_name || req.file.originalname;
  try {
    const client = await clientPromise;
    const result = await client.predict("/upload_file_secure", {
      user_id,
      password,
      filepath: req.file.buffer,
      custom_name
    });
    res.json({ data: result.data });
  } catch (err) {
    console.error("Upload error:", err);
    res.status(500).json({ error: "Upload failed", details: err.message });
  }
});

app.post("/api/download-link", async (req, res) => {
  const { user_id, password, filename } = req.body || {};

  if (!user_id || !password || !filename) {
    return res.status(400).json({ error: "Missing user_id, password, or filename" });
  }

  try {
    const client = await clientPromise;
    const result = await client.predict("/get_download_link_action", {
      user_id,
      password,
      filename,
    });
    res.json({ data: result.data });
  } catch (err) {
    console.error("Download-link error:", err);
    res.status(500).json({ error: "Download link failed", details: err.message });
  }
});

app.use(express.static("."));

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});