| 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): |
| |
| 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 |
| } |
| } |
| |
| |
| response = requests.post(api_url, headers=headers, json=json_data) |
| response_data = response.json() |
| |
| |
| 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))) |
| |
| |
| image_path = "temp_image." + image_format |
| image.save(image_path) |
| |
| return image_path |
| else: |
| return "No image received from API." |
|
|
| |
| 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" |
| ) |
|
|
| |
| iface.launch() |
|
|