hugging-c-ai / src /app /api /gradio /route.js
NeoPy's picture
feat: Hugging-c-ai v2 — HF OAuth + multi-provider AI + HF bucket storage + Gradio
60faa09
Raw History Blame Contribute Delete
1.91 kB
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 });
}
}