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).