--- license: mit tags: - nextjs - multi-provider - openai - anthropic - huggingface - groq - gradio - character-ai - chat - roleplay - hugging-c-ai --- # ๐Ÿค— Hugging-c-ai โ€” Multi-Provider AI Persona Chat Portal > Forked & heavily upgraded from [Open Character AI](https://github.com/Anil-matcha/open-character-ai). Replaces Google OAuth with **HuggingFace OAuth**, adds **multi-provider LLM support** (OpenAI, Anthropic, HuggingFace Inference, Groq, MuAPI, custom Gradio Servers), stores uploads in **HF Dataset buckets**, and supports **real-time streaming responses**. ## ๐Ÿš€ What's New (vs upstream) | Area | Upstream | Hugging-c-ai | |---|---|---| | **Auth** | Google OAuth + MuAPI API key | **HuggingFace OAuth** (with `repo:write` scope) + multi-provider API key (auto-detected) | | **LLM providers** | MuAPI gateway only | **6 providers**: OpenAI, Anthropic, HuggingFace, Groq, MuAPI, **custom Gradio Servers** | | **Streaming** | โŒ polling every 1.5s | โœ… **SSE streaming** for OpenAI, Anthropic, HF, Groq | | **Storage** | MuAPI CDN only | โœ… **HF Dataset bucket** (HfFileSystem) โ€” uploads save to user's own HF repo | | **Models** | 4 hardcoded options | โœ… **17 models** across 5 providers + custom Gradio endpoints | | **Database** | PostgreSQL (requires Supabase/Neon) | โœ… **SQLite by default** (zero setup) โ€” switch to Postgres for prod | | **Credit cost** | Flat 2 credits | โœ… **Per-model pricing** from catalog (1cโ€“15c) | | **Token tracking** | โŒ | โœ… Prompt + completion tokens logged per message | | **Marketplace** | โŒ | โœ… **`/explore`** page โ€” browse all public custom characters | | **Deploy button** | Vercel clone | โœ… **Duplicate to HF Space** | --- ## โœจ Core Features ### ๐Ÿ’ฌ Multi-Provider Chat Studio (`/[character_name]/[id]`) - **17 LLM options** grouped by provider in the model selector - **Real-time SSE streaming** โ€” watch tokens appear as they generate - **Custom Gradio endpoints** โ€” connect to any HF Space running a Gradio app - **Per-chat LLM tuning**: temperature, max tokens, reasoning mode, streaming toggle - **Vision support** for OpenAI GPT-4o, Claude 3.5, Gemini (via MuAPI) - Token usage tracking (prompt + completion tokens per message) ### ๐Ÿค— HuggingFace Authentication - OAuth2 flow with `openid profile email repo:write read` scopes - The user's HF access token is reused for: - Writing uploads to their own HF dataset repo - Calling private/gated Gradio Spaces - HuggingFace Inference API calls - Fallback: API key auth supports `hf_*`, `sk-*`, `sk-ant*`, `gsk_*`, `mu_*` prefixes โ€” auto-detected ### ๐Ÿ—„๏ธ HF Dataset Bucket Storage - Uploads are written to a HF dataset repo (default: `/hugging-c-ai-assets`) - Uses `@huggingface/hub`'s `uploadFile` / `listFiles` / `downloadFile` / `deleteFile` - The repo is created lazily on first upload if it doesn't exist - Public resolve URLs (`https://huggingface.co/datasets//resolve/main/`) are stored in the DB - Falls back to MuAPI upload if no HF token is available ### ๐Ÿ”Œ Gradio Server Support - Connect to any deployed Gradio app as a custom LLM endpoint - Format: `gradio:` or `gradio:::` - Uses the user's HF OAuth token to authenticate (so private/gated Spaces work) - Auto-tries common parameter shapes (chat, predict, single-message) - Reference: [Introducing Gradio Server](https://huggingface.co/blog/introducing-gradio-server) ### ๐ŸŽญ Character Builder & Marketplace - 15 preset characters (Einstein, Jobs, Sherlock, etc.) ship out of the box - Visual builder for custom personas โ€” name, avatar, system prompt, greeting, visibility - `/explore` page โ€” browse all public custom characters created by the community - Per-character public/private toggle (only visible to creator if private) ### ๐Ÿ’ณ Stripe Billing (optional) - 4 credit pack plans ($5/$10/$20/$50) - Pay-as-you-go credit balance, instantly updated via Stripe webhooks - Per-model cost deducted per message; auto-refunded on generation failure --- ## ๐Ÿ› ๏ธ Architecture ``` hugging-c-ai/ โ”œโ”€โ”€ prisma/ โ”‚ โ””โ”€โ”€ schema.prisma # SQLite schema (User, Account, Character, Chat, Message, UserImage, Creation) โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ app/ โ”‚ โ”‚ โ”œโ”€โ”€ page.js # Dashboard / character selection & builder โ”‚ โ”‚ โ”œโ”€โ”€ login/page.js # HF OAuth + API key tabs โ”‚ โ”‚ โ”œโ”€โ”€ explore/page.js # Public character marketplace โ”‚ โ”‚ โ”œโ”€โ”€ pricing/page.js # Stripe credit packs โ”‚ โ”‚ โ”œโ”€โ”€ [character_name]/[id]/page.js # Chat studio with streaming + multi-provider model selector โ”‚ โ”‚ โ””โ”€โ”€ api/ โ”‚ โ”‚ โ”œโ”€โ”€ auth/[...nextauth]/ # NextAuth handler (HF + Credentials) โ”‚ โ”‚ โ”œโ”€โ”€ characters/ # GET/POST/PATCH (15 defaults + custom) โ”‚ โ”‚ โ”œโ”€โ”€ chats/ # GET/POST chat threads โ”‚ โ”‚ โ”œโ”€โ”€ chats/[id]/messages/ # GET/POST messages โ†’ calls provider registry (supports SSE streaming) โ”‚ โ”‚ โ”œโ”€โ”€ models/ # GET model catalog grouped by provider โ”‚ โ”‚ โ”œโ”€โ”€ upload/ # POST file โ†’ HF dataset bucket (or MuAPI fallback) โ”‚ โ”‚ โ”œโ”€โ”€ images/ # GET user's upload history โ”‚ โ”‚ โ”œโ”€โ”€ hf/datasets/ # GET user's HF datasets + files in default repo โ”‚ โ”‚ โ”œโ”€โ”€ gradio/ # GET user's HF Spaces ยท POST probe a Gradio app's API โ”‚ โ”‚ โ”œโ”€โ”€ user/apikey/ # POST/DELETE custom API key โ”‚ โ”‚ โ”œโ”€โ”€ checkout/ # POST planId โ†’ Stripe checkout โ”‚ โ”‚ โ””โ”€โ”€ stripe/ # checkout + webhook (legacy inline) โ”‚ โ”œโ”€โ”€ lib/ โ”‚ โ”‚ โ”œโ”€โ”€ auth.js # NextAuth config (HF OAuth + Credentials) โ”‚ โ”‚ โ”œโ”€โ”€ prisma.js # PrismaClient + libsql adapter (SQLite) โ”‚ โ”‚ โ”œโ”€โ”€ config.js # App config + provider key map + Stripe plans โ”‚ โ”‚ โ”œโ”€โ”€ stripe.js # Lazy Stripe singleton โ”‚ โ”‚ โ”œโ”€โ”€ providers/huggingface.js # HF OAuth2 provider for NextAuth โ”‚ โ”‚ โ”œโ”€โ”€ ai/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ models.js # 17-model catalog + PROVIDER_META + getModelMeta() โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ providers.js # callLLM() โ€” single entry point, routes to adapter โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ openai.js # OpenAI SDK + streaming + vision โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ anthropic.js # Anthropic SDK + streaming + vision โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ huggingface.js # HF Inference (chat-completion) + streaming โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ groq.js # Groq SDK + streaming (ultra-low-latency) โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ muapi.js # Legacy MuAPI gateway (polling-based) โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ gradio.js # @gradio/client wrapper for custom Spaces โ”‚ โ”‚ โ”œโ”€โ”€ storage/hf.js # HFStorage class โ€” wraps @huggingface/hub โ”‚ โ”‚ โ””โ”€โ”€ services/ # billing.js, user.js (credit ledger) โ”‚ โ””โ”€โ”€ components/ # Navbar (HF branding), Footer โ”œโ”€โ”€ prisma.config.ts # Prisma 7 config (datasource URL) โ”œโ”€โ”€ next.config.mjs # Image remotePatterns for HF + muapi CDN โ”œโ”€โ”€ Dockerfile # HF Space Docker build โ”œโ”€โ”€ .env.example # All required env vars โ””โ”€โ”€ dev.db # SQLite DB (auto-created on first run) ``` --- ## ๐ŸŽฏ Quick Start ### 1. Install dependencies ```bash git clone https://huggingface.co/NeoPy/hugging-c-ai cd hugging-c-ai npm install ``` ### 2. Configure environment ```bash cp .env.example .env ``` Open `.env` and fill in: - **HuggingFace OAuth**: create at https://huggingface.co/settings/applications/new (callback URL: `http://localhost:3000/api/auth/callback/huggingface`) - **At least one AI provider key** (OpenAI, Anthropic, Groq, or HF_TOKEN) - (Optional) Stripe keys if you want to sell credits ### 3. Initialize the database ```bash npx prisma db push npx prisma generate ``` This creates a SQLite file at `./dev.db`. No external database required! ### 4. Run the dev server ```bash npm run dev ``` Open [http://localhost:3000](http://localhost:3000), click **Continue with HuggingFace**, and start chatting. --- ## ๐Ÿงฉ Adding a New LLM Provider The provider abstraction is in `src/lib/ai/`. To add a new provider: 1. **Create the adapter** at `src/lib/ai/.js`: ```js export async function callMyProvider({ model, messages, systemPrompt, temperature, maxTokens, stream, apiKey, onToken }) { // ... call your LLM SDK return { text, usage: { promptTokens, completionTokens } }; } ``` 2. **Register it** in `src/lib/ai/providers.js`: ```js case "myprovider": return callMyProvider({ ... }); ``` 3. **Add models** to the catalog in `src/lib/ai/models.js`: ```js { id: "myprovider:my-model", label: "My Model", provider: "myprovider", cost: 1, supportsStreaming: true, contextWindow: 32_000 } ``` 4. **Add env var mapping** in `src/lib/ai/providers.js` โ†’ `resolveApiKey()` and in `src/lib/config.js`. That's it โ€” the model appears in the UI selector automatically. --- ## ๐ŸŽจ Themes Five built-in dark themes via `NEXT_PUBLIC_THEME`: - `slate-indigo` (default) - `cyberpunk` (pink/cyan) - `emerald` (green) - `sunset` (orange) - `midnight` (pure B/W) --- ## ๐Ÿš€ Deploy to HuggingFace Spaces 1. Click **Duplicate to HF Space** in the navbar (top-right) 2. In your duplicated Space, set the following secrets: - `DATABASE_URL` โ€” `file:./dev.db` for SQLite, or a Postgres URL - `HF_CLIENT_ID`, `HF_CLIENT_SECRET` โ€” your HF OAuth app (callback URL must point to your Space) - `NEXTAUTH_SECRET` โ€” `openssl rand -base64 32` - `NEXTAUTH_URL` โ€” `https://.hf.space` - Provider keys: `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, `GROQ_API_KEY`, `HF_TOKEN` (at least one) 3. Run `npx prisma db push && npx prisma generate` once (in the Space terminal) For production at scale, swap SQLite for Postgres by setting `DATABASE_URL` to a `postgresql://` URL and switching the Prisma adapter in `src/lib/prisma.js` to `@prisma/adapter-pg`. --- ## ๐Ÿ“œ License MIT. Upstream credit: [Open Character AI](https://github.com/Anil-matcha/open-character-ai).