geeksiddhant/comfyUI
0
1import requests2import gradio as gr3import base644from PIL import Image5import io6import os7 8# Define a function to decode the base64 image and save it as a file9def save_image(base64_str, image_format='png'):10 image_data = base64.b64decode(base64_str)11 image = Image.open(io.BytesIO(image_data))12 # Generate a unique filename13 filename = f"output_image.{image_format}"14 image.save(filename)15 return filename16 17# Define the function that will call the API and handle the image18def query_api(negative_prompt, positive_prompt):19 # Prepare the headers and the JSON body of the request20 headers = {21 "Authorization": "Api-Key O7NlgQG7.9e3nxz7K4K3CakdBpSYkHNlGx5gOmaXb"22 }23 json_data = {24 'workflow_values': {25 'negative_prompt': negative_prompt,26 'positive_prompt': positive_prompt27 }28 }29 30 # Send the POST request31 response = requests.post(32 "https://model-5qe9kjp3.api.baseten.co/development/predict",33 headers=headers,34 json=json_data35 )36 37 # Process the response38 if response.status_code == 200:39 data = response.json()40 results = data.get('result', [])41 if results:42 # Assuming the first result is what we want to display43 base64_image = results[0].get('data')44 image_format = results[0].get('format', 'png')45 if base64_image and image_format:46 # Save the decoded image47 image_path = save_image(base64_image, image_format)48 return image_path49 else:50 return "No image data found in the API response."51 else:52 return "The API response did not contain any results."53 else:54 return "Failed to fetch data from the API."55 56# Define the Gradio interface57iface = gr.Interface(58 fn=query_api,59 inputs=[60 gr.Textbox(label="Negative Prompt", placeholder="Enter Negative Prompt Here"),61 gr.Textbox(label="Positive Prompt", placeholder="Enter Positive Prompt Here")62 ],63 outputs=gr.File(label="Download Image", type="filepath"),64 title="API Query Interface without ControlNet Image",65 description="Enter a negative prompt and a positive prompt to query the API and download the resulting image."66)67 68# Launch the Gradio app69iface.launch()70 