Spaces:
Running
Running
import os | |
import requests | |
import gradio as gr | |
# Define the function to query Hugging Face API for image generation | |
def generate_image(prompt): | |
API_URL = "https://api-inference.huggingface.co/models/KingNish/flux-me" | |
API_TOKEN = os.getenv("HF_READ_TOKEN") # Ensure the token is set in your environment | |
headers = {"Authorization": f"Bearer {API_TOKEN}"} | |
payload = { | |
"inputs": prompt | |
} | |
# Call the Hugging Face API to generate the image | |
response = requests.post(API_URL, headers=headers, json=payload) | |
# Check if the request was successful | |
if response.status_code != 200: | |
return f"Error: {response.status_code}, {response.text}" | |
# Save the generated image | |
image_path = "generated_image.png" | |
with open(image_path, "wb") as f: | |
f.write(response.content) | |
return image_path | |
# Define the chatbot function to return the generated image | |
def chatbot(prompt): | |
image = generate_image(prompt) | |
return image | |
# Create the Gradio interface with the same UI/UX | |
interface = gr.Interface( | |
fn=chatbot, | |
inputs="text", | |
outputs="image", | |
title="prompthero/openjourney", | |
description="Enter a text prompt and get an AI-generated image." | |
) | |
# Launch the interface | |
interface.launch() | |