import gradio as gr import requests import base64 from PIL import Image import io import json import os import numpy #from dotenv import load_dotenv #dotenv_path = '/.env' #load_dotenv(dotenv_path) # Get the API key from the environment variable API_KEY = os.getenv('BASETEN_API_KEY') API_url = os.getenv('BASETEN_COMFY_MODEL_URL') def call_api_and_generate_image(negative_prompt, positive_prompt,seed,source_image_base64): if not API_KEY: return None, "Error: API key not found in environment variables. Please set BASETEN_API_KEY." # Convert the source image to base64 #buffered = io.BytesIO() #source_image.save(buffered, format="PNG") #img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') resp = requests.post( API_url, headers={"Authorization": f"Api-Key {API_KEY}"}, json={ 'workflow_values': { 'negative_prompt': negative_prompt, 'positive_prompt': positive_prompt, 'seed': seed, 'source_image_fs': source_image_base64 } } ) result = resp.json() print(result) base64_data = result['result'][0]['data'] image_data = base64.b64decode(base64_data) image = Image.open(io.BytesIO(image_data)) return image#, json.dumps(result, indent=2) def generate_image(positive_prompt, negative_prompt,source_image): seed = numpy.random.randint(0, 2**32 - 1) source_image_base64 = None if source_image is not None: with open(source_image, "rb") as image_file: source_image_base64 = base64.b64encode(image_file.read()).decode('utf-8') try: return call_api_and_generate_image(negative_prompt, positive_prompt,seed,source_image_base64) except Exception as e: return None#, f"Error: {str(e)}" def clear_fields(): return "", "", None # Clear prompt and outputs with gr.Blocks(theme='freddyaboulton/test-blue') as demo: gr.Markdown("