File size: 8,066 Bytes
682910a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Piclets Server API Documentation

## Overview

The Piclets Server provides a Gradio-based API for saving and sharing AI-generated creatures (Piclets) from the Piclets game. This backend supports the main game client with persistent storage and sharing capabilities.

## Quick Start

### Running Locally
```bash
pip install -r requirements.txt
python app.py
```

### Accessing the API
- **Web Interface**: http://localhost:7860
- **Programmatic Access**: Use Gradio Client to connect to the space

## API Endpoints

### 1. Save Piclet
**Endpoint**: `/save_piclet_api`
**Method**: Gradio function call
**Purpose**: Save a new Piclet with optional image

**Input Parameters**:
- `piclet_json` (string): JSON-formatted Piclet data
- `image_file` (file, optional): Image file for the Piclet

**Response Format**:
```json
{
  "success": true,
  "piclet_id": "uuid-string",
  "message": "Piclet saved successfully"
}
```

**Error Response**:
```json
{
  "success": false,
  "error": "Error description"
}
```

### 2. Get Piclet
**Endpoint**: `/get_piclet_api`
**Method**: Gradio function call
**Purpose**: Retrieve a Piclet by ID

**Input Parameters**:
- `piclet_id` (string): Unique Piclet identifier

**Response Format**:
```json
{
  "success": true,
  "piclet": {
    "id": "uuid-string",
    "name": "Piclet Name",
    "description": "Description",
    "tier": "medium",
    "primaryType": "space",
    "baseStats": { ... },
    "movepool": [ ... ],
    "created_at": "2024-07-26T10:30:00"
  }
}
```

### 3. List Piclets
**Endpoint**: `/list_piclets_api`
**Method**: Gradio function call
**Purpose**: Get a list of all saved Piclets

**Input Parameters**:
- `limit` (integer): Maximum number of results (1-100, default: 50)

**Response Format**:
```json
{
  "success": true,
  "piclets": [
    {
      "id": "uuid-string",
      "name": "Piclet Name",
      "primaryType": "space",
      "tier": "medium",
      "created_at": "2024-07-26T10:30:00",
      "has_image": true
    }
  ],
  "count": 1
}
```

### 4. Get Piclet Image
**Endpoint**: `/get_piclet_image_api`
**Method**: Gradio function call
**Purpose**: Retrieve the image associated with a Piclet

**Input Parameters**:
- `piclet_id` (string): Unique Piclet identifier

**Response**: Image file or None if not found

## Required JSON Format for Piclets

When saving a Piclet, the JSON must include these required fields:

```json
{
  "name": "Stellar Wolf",
  "description": "A majestic wolf creature infused with cosmic energy",
  "tier": "medium",
  "primaryType": "space",
  "secondaryType": "beast",
  "baseStats": {
    "hp": 85,
    "attack": 90,
    "defense": 70,
    "speed": 80
  },
  "nature": "Bold",
  "specialAbility": {
    "name": "Cosmic Howl",
    "description": "Increases attack when health is low"
  },
  "movepool": [
    {
      "name": "Star Strike",
      "type": "space",
      "power": 75,
      "accuracy": 90,
      "pp": 15,
      "priority": 0,
      "flags": [],
      "effects": [
        {
          "type": "damage",
          "target": "opponent",
          "amount": "normal"
        }
      ]
    }
  ]
}
```

### Field Specifications

#### Required Fields
- **name**: String - Piclet's name
- **primaryType**: String - One of: beast, bug, aquatic, flora, mineral, space, machina, structure, culture, cuisine
- **baseStats**: Object with hp, attack, defense, speed (integers)
- **movepool**: Array of move objects (minimum 1 move)

#### Optional Fields
- **description**: String - Flavor text
- **tier**: String - low, medium, high, legendary
- **secondaryType**: String - Same options as primaryType or null
- **nature**: String - Personality trait
- **specialAbility**: Object with name and description

#### Move Object Format
```json
{
  "name": "Move Name",
  "type": "attack_type",
  "power": 75,
  "accuracy": 90,
  "pp": 15,
  "priority": 0,
  "flags": ["contact", "bite"],
  "effects": [
    {
      "type": "damage",
      "target": "opponent",
      "amount": "normal"
    }
  ]
}
```

## Frontend Integration

### Using Gradio Client (JavaScript)

```javascript
// Connect to the Piclets server
const client = await window.gradioClient.Client.connect("your-hf-space-name");

// Save a Piclet
async function savePiclet(picletData, imageFile = null) {
  const result = await client.predict("/save_piclet_api", [
    JSON.stringify(picletData),
    imageFile
  ]);
  return JSON.parse(result.data[0]);
}

// Get a Piclet
async function getPiclet(picletId) {
  const result = await client.predict("/get_piclet_api", [picletId]);
  return JSON.parse(result.data[0]);
}

// List Piclets
async function listPiclets(limit = 20) {
  const result = await client.predict("/list_piclets_api", [limit]);
  return JSON.parse(result.data[0]);
}

// Get Piclet Image
async function getPicletImage(picletId) {
  const result = await client.predict("/get_piclet_image_api", [picletId]);
  return result.data[0]; // Returns image path or null
}
```

### Example Usage

```javascript
// Example Piclet data
const picletData = {
  name: "Thunder Drake",
  primaryType: "space",
  baseStats: { hp: 90, attack: 85, defense: 75, speed: 95 },
  movepool: [
    {
      name: "Lightning Bolt",
      type: "space",
      power: 80,
      accuracy: 95,
      pp: 15,
      priority: 0,
      flags: [],
      effects: [{ type: "damage", target: "opponent", amount: "normal" }]
    }
  ]
};

// Save the Piclet
const saveResult = await savePiclet(picletData);
if (saveResult.success) {
  console.log(`Piclet saved with ID: ${saveResult.piclet_id}`);
  
  // Retrieve it back
  const retrievedPiclet = await getPiclet(saveResult.piclet_id);
  console.log(retrievedPiclet);
}
```

## Storage Structure

The server uses a file-based storage system:

```
data/
β”œβ”€β”€ piclets/           # Individual Piclet JSON files
β”‚   β”œβ”€β”€ uuid1.json
β”‚   β”œβ”€β”€ uuid2.json
β”‚   └── ...
β”œβ”€β”€ images/            # Piclet images
β”‚   β”œβ”€β”€ uuid1.png
β”‚   β”œβ”€β”€ uuid2.jpg
β”‚   └── ...
└── collections/       # Future: Shared collections
    └── ...
```

## Error Handling

All API endpoints return consistent error formats:

```json
{
  "success": false,
  "error": "Descriptive error message"
}
```

### Common Errors
- **Missing required field**: When JSON is missing name, primaryType, baseStats, or movepool
- **Invalid JSON format**: When the input string is not valid JSON
- **Piclet not found**: When requesting a non-existent Piclet ID
- **File system errors**: Issues with saving/reading files

## Deployment to Hugging Face Spaces

1. **Push to Repository**: The space automatically deploys when you push to the connected git repository
2. **Environment**: Python 3.9+ with Gradio 5.38.2
3. **Persistent Storage**: Data persists between space restarts
4. **Public Access**: The API is accessible to anyone with the space URL

### Space Configuration (in README.md)
```yaml
---
title: Piclets Server
emoji: πŸ¦€
colorFrom: yellow
colorTo: pink
sdk: gradio
sdk_version: 5.38.2
app_file: app.py
pinned: false
---
```

## Future Enhancements

### Planned Features
- **Collections**: Save and share groups of Piclets
- **User Authentication**: Personal Piclet collections
- **Battle Statistics**: Track battle performance
- **Online Multiplayer**: Real-time battle coordination
- **Search and Filtering**: Find Piclets by type, tier, name
- **Import/Export**: Bulk operations

### API Extensions
- `create_collection_api(name, piclet_ids)`
- `get_collection_api(collection_id)`
- `search_piclets_api(query, filters)`
- `get_battle_stats_api(piclet_id)`

## Security Considerations

- **Input Validation**: All JSON inputs are validated
- **File Size Limits**: Images are limited by Gradio defaults
- **Rate Limiting**: Handled by Hugging Face Spaces infrastructure
- **Content Filtering**: Future enhancement for inappropriate content

## Support

For issues or questions:
1. Check the error messages in API responses
2. Verify JSON format matches the specification
3. Ensure all required fields are present
4. Test with the example data provided