Spaces:
Sleeping
Sleeping
File size: 1,121 Bytes
b3a7848 |
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 |
# application/utils/image_generation.py - NEW FILE
from huggingface_hub import InferenceClient
import os
import base64
from io import BytesIO
def generate_image(prompt: str) -> str:
"""
Generates an image based on the given prompt using the Hugging Face Inference API.
Args:
prompt: The text prompt to generate the image from.
Returns:
The base64 encoded image data, or None if an error occurred.
"""
try:
api_key = os.environ.get('auth')
if not api_key:
raise ValueError("Hugging Face API key ('auth') not found in environment variables.")
client = InferenceClient(api_key=api_key)
image = client.text_to_image(
prompt,
model="black-forest-labs/FLUX.1-schnell"
)
# Convert PIL Image to base64 string
buffered = BytesIO()
image.save(buffered, format="PNG") # Or "JPEG" if appropriate
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
return img_str
except Exception as e:
print(f"Error in image generation: {e}")
return None |