APIComfy / app.py
asrafnoor's picture
Update app.py
233cd18 verified
import gradio as gr
import requests
import base64
import os
from io import BytesIO
from PIL import Image
API_KEY = os.getenv("BT_IGAPI_KEY")
def call_api(negative_prompt, positive_prompt):
# Setting up the API request
api_url = "https://model-5wo8yvn3.api.baseten.co/development/predict"
headers = {"Authorization": API_KEY}
json_data = {
'workflow_values': {
'negative_prompt': negative_prompt,
'positive_prompt': positive_prompt
}
}
# Sending the POST request to the API
response = requests.post(api_url, headers=headers, json=json_data)
response_data = response.json()
# Decoding the base64 encoded image
if response_data['result']:
base64_image = response_data['result'][0]['data']
image_format = response_data['result'][0]['format']
image = Image.open(BytesIO(base64.b64decode(base64_image)))
# Saving the image temporarily to display in Gradio UI
image_path = "temp_image." + image_format
image.save(image_path)
return image_path
else:
return "No image received from API."
# Creating the Gradio interface
iface = gr.Interface(
fn=call_api,
inputs=[
gr.Textbox(label="Negative Prompt"),
gr.Textbox(label="Positive Prompt")
],
outputs=gr.Image(type="filepath"),
title="API Image Generator",
description="Enter Negative and Positive Prompts to generate an image via the API"
)
# Launching the Gradio app
iface.launch()