Your question begs the answer. An AI cannot learn, legally speaking. It is not a legally recognized actor. The person building or operating it is who is involved here. Much like legally the photographer is involved in copyright law rather than the camera.
Once framed correctly from a legal perspective, you have a person creating a tool using copyrighted material. Is this legal? For images, probably. However, selling or renting the tool or images generated using it is an open question. You can legally photograph a copyrighted image using a camera. But you cannot sell the photograph without permission from the original rights holder, because that would violate their copyright. And things are different for copyrighted text, such as a book (and computer source code?). You can only legally photocopy a portion of a book as fair use. Copying an entire book without permission is a copyright violation.
Once framed correctly from a legal perspective, you have a person creating a tool using copyrighted material. Is this legal? For images, probably. However, selling or renting the tool or images generated using it is an open question. You can legally photograph a copyrighted image using a camera. But you cannot sell the photograph without permission from the original rights holder, because that would violate their copyright. And things are different for copyrighted text, such as a book (and computer source code?). You can only legally photocopy a portion of a book as fair use. Copying an entire book without permission is a copyright violation.