ahmadalfakeh's picture
Update app.py
e3724be verified
import gradio as gr
import requests
import io
from PIL import Image
import os
# Access the token from the environment secrets
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} # No need to hard-code the token
def query(payload):
# Make the API request using the token from environment secrets
response = requests.post(API_URL, headers=headers, json=payload)
# Check if the response is successful
if response.status_code != 200:
raise Exception(f"Failed to generate image: {response.status_code}, {response.text}")
return response.content
def generate_image(prompt):
# Query the model with the user-provided prompt
image_bytes = query({"inputs": prompt})
# Open the image from the bytes received
image = Image.open(io.BytesIO(image_bytes))
return image
# Create a Gradio interface
interface = gr.Interface(
fn=generate_image, # Function to call
inputs="text", # Input will be a text box
outputs="image", # Output will be an image
title="Hugging Face Image Generator", # Title for the Gradio space
description="Enter a prompt to generate an image using the Hugging Face model."
)
# Launch the app
interface.launch()