Integración de OpenAI Vision para renombrar fotos automáticamente
Clase 12 de 27 • Curso de lanzamiento y monetización de webs con Lovable
Resumen
Automate photo naming with confidence: learn how API calls, OpenAI Vision, and an edge function work together to rename images securely, update storage and database records, and keep the UI responsive.
Why use API calls and OpenAI Vision for photo renaming?
Integrating OpenAI Vision via API calls enables automatic, descriptive file names based on the photo’s content. The process is designed to be secure, avoid exposing the API key, and ensure the new name is reflected on the front end, in storage, and in the database.
What does the AI flow look like?
- Add a “Rename Files” button in the photo gallery on the dashboard.
- Send the selected photo to an edge function.
- Call OpenAI Vision to analyze the image.
- Receive a suggested name and rename the file.
- Update the UI state and persist the change in storage and database.
Which components and data are updated?
- The photo gallery component displays the new name.
- The storage bucket file name is updated.
- The photos table records reflect the change.
- The UI includes loading states and progress indicators.
How to implement the edge function and front end flow?
A clear plan drives a reliable implementation. Using chat mode in Lovable, a precise prompt defined the target component, the desired button, the edge function call, and the required updates across the system. The result: a Supabase edge function named “AI Photo Rename” that uses OpenAI’s vision model, plus front end support for batch actions and error handling.
How to craft effective prompts in chat mode?
- Name the target: “in the photo gallery component” on the “dashboard.”
- Define the trigger: a button labeled Rename Files.
- Specify the flow: send to an edge function that calls OpenAI Vision.
- State side effects: update front end, storage, and database names.
- Ask for resilience: include error handling and retry logic.
What secure setup is required?
- Use an OpenAI API key and keep it private.
- Store the key securely; do not hardcode or expose it.
- Verify account credits before running the flow.
- Ensure the edge function has access to the key at runtime.
What success looks like?
- The rename completes and shows a clear confirmation.
- Examples produced by the model: “bored breakfast.jpg” and “bored eating cereal.jpg.”
- Optional improvement noted: add real time updates to reflect changes instantly.
What issues appeared and how were they solved securely?
Real-world integration surfaced small but important fixes. A hover effect prevented clicking the AI button due to layering; raising the button layer made it reliably clickable. An initial failure seemed related to large image size, but the actual cause was no credits on the OpenAI account. After securing credits and confirming the key, the rename flow worked end to end.
What vocabulary should you notice?
- API calls: requests from the app to external services.
- OpenAI Vision: model that reads an image and suggests a name.
- Edge function: server-side function (e.g., Supabase) that runs close to users for speed and security.
- Photo gallery component: UI section where images and actions appear.
- Batch: apply actions to multiple items.
- Loading states / progress indicators: UI feedback during processing.
- Storage bucket: file storage where the image lives.
- Database records: rows (e.g., in a photos table) storing metadata like file name.
- Error handling / retry logic: recover from failures and try again safely.
- Hover effect / layering: visual effect and z-order that can block clicks.
- Real time: automatic UI updates without manual refresh.
- Credits: account balance required to use the OpenAI API.
Have a question about the prompt, the edge function, or securing the API key? Share your scenario and what you’ve tried—let’s troubleshoot together.