File size: 1,908 Bytes
60faa09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { client } from "@gradio/client";

export async function GET() {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    const token = session.user.hfAccessToken;
    const username = session.user.hfUsername;
    if (!token || !username) {
      return NextResponse.json({ spaces: [], note: "Sign in with HuggingFace to see your Spaces." });
    }

    const res = await fetch(
      `https://huggingface.co/api/spaces?author=${encodeURIComponent(username)}`,
      { headers: { Authorization: `Bearer ${token}` } },
    );
    let spaces = [];
    if (res.ok) spaces = await res.json();

    return NextResponse.json({
      spaces: spaces.map((s) => ({
        id: s.id, name: s.name,
        url: `https://${s.id.replace("/", "-")}.hf.space`,
        runtime: s.runtime,
      })),
    });
  } catch (error) {
    console.error("[GRADIO_SPACES_GET_ERROR]", error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}

export async function POST(req) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    const { url } = await req.json();
    if (!url) return NextResponse.json({ error: "url is required" }, { status: 400 });

    const app = await client(url, {
      hfToken: session.user.hfAccessToken?.startsWith("hf_") ? session.user.hfAccessToken : undefined,
    });
    const info = app.api_info;
    return NextResponse.json({ url, api_info: info });
  } catch (error) {
    console.error("[GRADIO_PROBE_ERROR]", error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}