react-a11y-auto-caption 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +152 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# react-a11y-auto-caption
|
|
2
|
+
|
|
3
|
+
> A smart React & Next.js component <br/>
|
|
4
|
+
that automatically generates highly accurate `alt` text for images using AI.<br/>
|
|
5
|
+
**Generate captions effortlessly during local development, <br/>
|
|
6
|
+
save them to your database, and serve 100% accessible images in production<br/>
|
|
7
|
+
with zero API costs and zero latency.**
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/react-a11y-auto-caption)
|
|
10
|
+
[](https://opensource.org/licenses/MIT)
|
|
11
|
+
[]()
|
|
12
|
+
|
|
13
|
+
## Why use this library?
|
|
14
|
+
|
|
15
|
+
- **Generate Once, Serve Forever (Best Practice):** Automatically generate captions during local development, easily save them to your database using the `onCaptionGenerated` callback, and completely bypass AI requests in production.
|
|
16
|
+
- **Cost-Free Local AI Server:** Comes with a ready-to-use, lightweight FastAPI Python server. It runs perfectly on your local machine, meaning you don't need to pay for expensive cloud GPUs or third-party API subscriptions (like OpenAI).
|
|
17
|
+
- **Zero-Config Accessibility:** Automatically describes images for screen readers without manual data entry.
|
|
18
|
+
- **First-Class Next.js Support:** Provides a dedicated `<SmartNextImage>` component optimized for Next.js.
|
|
19
|
+
- **Smart Request Caching:** Built-in memory caching prevents duplicate API calls for the same image, saving your server costs.
|
|
20
|
+
- **Concurrent Request Defense:** Safely handles simultaneous renders of the same image across multiple components.
|
|
21
|
+
- **Global Provider Pattern:** Set your API endpoint once at the root level and forget about it.
|
|
22
|
+
- **Developer Experience (DX):** Built-in test mode (`disableAI`) and intelligent console warnings for smooth debugging.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
npm
|
|
29
|
+
```bash
|
|
30
|
+
npm install react-a11y-auto-caption
|
|
31
|
+
```
|
|
32
|
+
yarn berry
|
|
33
|
+
```bash
|
|
34
|
+
yarn add react-a11y-auto-caption
|
|
35
|
+
```
|
|
36
|
+
pnpm
|
|
37
|
+
```bash
|
|
38
|
+
pnpm add react-a11y-auto-caption
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
### 1. Wrap your app with the Provider (Optional but Recommended) <br/>
|
|
45
|
+
Set your backend API endpoint once globally. <br/>
|
|
46
|
+
Otherwise, you can simply pass apiEndpoint as a prop when using `<SmartImage>` <br/>
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// App.tsx or layout.tsx
|
|
50
|
+
import { SmartImageProvider } from 'react-a11y-auto-caption';
|
|
51
|
+
|
|
52
|
+
export default function App({ children }) {
|
|
53
|
+
return (
|
|
54
|
+
<SmartImageProvider value={{ apiEndpoint: "https://your-api.com/api/generate-caption" }}>
|
|
55
|
+
{children}
|
|
56
|
+
</SmartImageProvider>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
### 2. Use it in your components
|
|
61
|
+
For Vanilla React (Vite, CRA):<br/>
|
|
62
|
+
```ts
|
|
63
|
+
import { SmartImage } from 'react-a11y-auto-caption';
|
|
64
|
+
|
|
65
|
+
function Gallery() {
|
|
66
|
+
return (
|
|
67
|
+
<SmartImage
|
|
68
|
+
src="[https://example.com/beautiful-landscape.jpg](https://example.com/beautiful-landscape.jpg)"
|
|
69
|
+
announceLive={true}
|
|
70
|
+
/>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
For Next.js: <br/>
|
|
76
|
+
```ts
|
|
77
|
+
import { SmartNextImage } from 'react-a11y-auto-caption';
|
|
78
|
+
import dogPic from '../public/dog.jpg';
|
|
79
|
+
|
|
80
|
+
function NextGallery() {
|
|
81
|
+
return (
|
|
82
|
+
<SmartNextImage
|
|
83
|
+
src={dogPic}
|
|
84
|
+
width={500}
|
|
85
|
+
height={300}
|
|
86
|
+
// You can override global settings per component
|
|
87
|
+
disableAI={process.env.NODE_ENV === 'development'}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## API Reference
|
|
95
|
+
Both `SmartImage` and `SmartNextImage` inherit all standard `<img>` / `next/image` props, plus the following: <br/>
|
|
96
|
+
## API Reference (Props)
|
|
97
|
+
|
|
98
|
+
Both `<SmartImage>` and `<SmartNextImage>` inherit all standard HTML `<img>` (or `next/image`) attributes. The following custom props are available:
|
|
99
|
+
|
|
100
|
+
| Prop | Type | Default | Description |
|
|
101
|
+
| :--- | :--- | :--- | :--- |
|
|
102
|
+
| `apiEndpoint` | `string` | `undefined` | The URL of your AI backend API. Overrides the `SmartImageProvider`'s endpoint if provided. |
|
|
103
|
+
| `alt` | `string` | `undefined` | Manual alt text. If provided, the AI generation is completely bypassed. |
|
|
104
|
+
| `fallbackAlt` | `string` | `"Image loading or caption unavailable"` | The text displayed if the AI API request fails or times out. |
|
|
105
|
+
| `disableAI` | `boolean` | `false` | Disables AI generation and uses a mock caption. Highly recommended for local development and testing. |
|
|
106
|
+
| `announceLive` | `boolean` | `false` | Enables `aria-live` regions to dynamically announce the generation status to screen readers. |
|
|
107
|
+
| `onCaptionGenerated`| `function` | `undefined` | Callback fired when a caption is successfully generated. `(caption: string) => void` |
|
|
108
|
+
|
|
109
|
+
> **Note:** If you are using `<SmartNextImage>`, you must also provide standard required Next.js image props such as `width` and `height` (unless using `fill`).
|
|
110
|
+
---
|
|
111
|
+
## Security & Backend Integration
|
|
112
|
+
|
|
113
|
+
This package requires a backend to process the images through an AI model (like ViT-GPT2). <br/>
|
|
114
|
+
We provide a ready-to-use FastAPI reference server in [this repository.](https://github.com/kong33/SmartImage)<br/>
|
|
115
|
+
|
|
116
|
+
Depending on your architecture, choose one of the following integration methods:<br/>
|
|
117
|
+
---
|
|
118
|
+
### First step: Deploying our Standalone AI Microservice
|
|
119
|
+
For security reasons, all cross-origin requests are blocked by default. <br/>
|
|
120
|
+
You MUST set the ALLOWED_ORIGINS environment variable in your server's .env file<br/>
|
|
121
|
+
to allow your frontend to communicate with it. <br/>
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
# For production
|
|
126
|
+
|
|
127
|
+
# .env file on your Python server
|
|
128
|
+
ALLOWED_ORIGINS=https://your-frontend-domain.com,https://your-frontend-domain2.com
|
|
129
|
+
```
|
|
130
|
+
```python
|
|
131
|
+
# For local development
|
|
132
|
+
|
|
133
|
+
# .env file on your Python server
|
|
134
|
+
ALLOWED_ORIGINS=http://localhost:3000
|
|
135
|
+
```
|
|
136
|
+
> **Note:** Change the port if you are using Vite 5173 or another local server. You can allow multiple environments simultaneously by separating them with a comma (no spaces).
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## Best Practice: Generate Once, Save, Reuse
|
|
141
|
+
Generating captions on-the-fly for every user is slow. <br/>
|
|
142
|
+
The industry standard is to generate the caption once and save it to your database:
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
<SmartImage
|
|
146
|
+
src={image.url}
|
|
147
|
+
alt={image.savedAlt} // If provided, the AI sleeps!
|
|
148
|
+
apiEndpoint="http://localhost:8000/api/generate-caption"
|
|
149
|
+
onCaptionGenerated={(text) => {
|
|
150
|
+
saveToDatabase(image.id, text); // Save permanently (You create your own database saving function based on your code.)
|
|
151
|
+
}}
|
|
152
|
+
/>
|
package/package.json
CHANGED