File size: 10,675 Bytes
109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e 60faa09 109df1e | 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | ---
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: `<hf-username>/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/<repo>/resolve/main/<path>`) 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:<space-url>` or `gradio:<space-url>::<api-name>`
- 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/<provider>.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://<your-space>.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).
|