Welcome to AI & Data Skills Shorts by CodeVisium ๐ In this project you will build a Text-to-Image AI Generator using Python. The application takes a text prompt and generates an AI image using a model like Stable Diffusion. You will learn: How AI image generation works How to call AI APIs using Python How to save generated images How to create a simple web UI ๐น Step 1: Install Required Libraries Run: pip install requests pillow streamlit Libraries used: requests Used to send API requests. pillow (PIL) Used for handling images. streamlit Used for building a quick web interface. ๐น Step 2: Create Text Prompt A prompt describes what image you want. Example: prompt = "astronaut riding a horse in space" Better prompts give better images. Examples: futuristic city skyline cyberpunk robot warrior fantasy castle on floating island ๐น Step 3: Generate Image using API Example Python script: import requests api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2" headers = { "Authorization": "Bearer YOUR_API_KEY" } data = { "inputs": "A futuristic neon city at night" } response = requests.post(api_url, headers=headers, json=data) with open("generated.png", "wb") as f: f.write(response.content) This code: Sends text prompt to AI model Model generates image Image is saved locally ๐น Step 4: Open Generated Image Using Python: from PIL import Image img = Image.open("generated.png") img.show() This will display the generated AI artwork. ๐น Step 5: Build Web Interface with Streamlit Create file app.py import streamlit as st import requests st.title("AI Image Generator") prompt = st.text_input("Enter prompt") if st.button("Generate Image"): response = requests.post( api_url, headers=headers, json={"inputs": prompt} ) st.image(response.content) Run app: streamlit run app.py Now you have a simple AI image generator web app. ๐ Tools Used in This Project Py
Original Description
Welcome to AI & Data Skills Shorts by CodeVisium ๐
In this project you will build a Text-to-Image AI Generator using Python.
The application takes a text prompt and generates an AI image using a model like Stable Diffusion.
You will learn:
How AI image generation works
How to call AI APIs using Python
How to save generated images
How to create a simple web UI
๐น Step 1: Install Required Libraries
Run:
pip install requests pillow streamlit
Libraries used:
requests
Used to send API requests.
pillow (PIL)
Used for handling images.
streamlit
Used for building a quick web interface.
๐น Step 2: Create Text Prompt
A prompt describes what image you want.
Example:
prompt = "astronaut riding a horse in space"
Better prompts give better images.
Examples:
futuristic city skyline
cyberpunk robot warrior
fantasy castle on floating island
๐น Step 3: Generate Image using API
Example Python script:
import requests
api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
headers = {
"Authorization": "Bearer YOUR_API_KEY"
}
data = {
"inputs": "A futuristic neon city at night"
}
response = requests.post(api_url, headers=headers, json=data)
with open("generated.png", "wb") as f:
f.write(response.content)
This code:
Sends text prompt to AI model
Model generates image
Image is saved locally
๐น Step 4: Open Generated Image
Using Python:
from PIL import Image
img = Image.open("generated.png")
img.show()
This will display the generated AI artwork.
๐น Step 5: Build Web Interface with Streamlit
Create file app.py
import streamlit as st
import requests
st.title("AI Image Generator")
prompt = st.text_input("Enter prompt")
if st.button("Generate Image"):
response = requests.post(
api_url,
headers=headers,
json={"inputs": prompt}
)
st.image(response.content)
Run app:
streamlit run app.py
Now you have a simple AI image generator web app.
๐ Tools Used in This Project
Py