File size: 2,338 Bytes
f19c84d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87131e4
f19c84d
 
 
 
 
 
 
 
 
 
cd7845a
f19c84d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import gradio as gr
import base64
from PIL import Image
import io
import os

# Define a function to decode the base64 image and save it as a file
def save_image(base64_str, image_format='png'):
    image_data = base64.b64decode(base64_str)
    image = Image.open(io.BytesIO(image_data))
    # Generate a unique filename
    filename = f"output_image.{image_format}"
    image.save(filename)
    return filename

# Define the function that will call the API and handle the image
def query_api(negative_prompt, positive_prompt):
    # Prepare the headers and the JSON body of the request
    headers = {
        "Authorization": "Api-Key O7NlgQG7.9e3nxz7K4K3CakdBpSYkHNlGx5gOmaXb"
    }
    json_data = {
        'workflow_values': {
            'negative_prompt': negative_prompt,
            'positive_prompt': positive_prompt
        }
    }
    
    # Send the POST request
    response = requests.post(
        "https://model-5qe9kjp3.api.baseten.co/development/predict",
        headers=headers,
        json=json_data
    )
    
    # Process the response
    if response.status_code == 200:
        data = response.json()
        results = data.get('result', [])
        if results:
            # Assuming the first result is what we want to display
            base64_image = results[0].get('data')
            image_format = results[0].get('format', 'png')
            if base64_image and image_format:
                # Save the decoded image
                image_path = save_image(base64_image, image_format)
                return image_path
            else:
                return "No image data found in the API response."
        else:
            return "The API response did not contain any results."
    else:
        return "Failed to fetch data from the API."

# Define the Gradio interface
iface = gr.Interface(
    fn=query_api,
    inputs=[
        gr.Textbox(label="Negative Prompt", placeholder="Enter Negative Prompt Here"),
        gr.Textbox(label="Positive Prompt", placeholder="Enter Positive Prompt Here")
    ],
    outputs=gr.File(label="Download Image", type="filepath"),
    title="API Query Interface without ControlNet Image",
    description="Enter a negative prompt and a positive prompt to query the API and download the resulting image."
)

# Launch the Gradio app
iface.launch()