geeksiddhant commited on
Commit
af6d6a4
·
verified ·
1 Parent(s): 962b00f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -51
app.py CHANGED
@@ -1,64 +1,57 @@
1
  import gradio as gr
2
  import requests
3
  import base64
4
- import logging
5
  from PIL import Image
6
- from io import BytesIO
 
7
 
8
- # Set up logging to display debug information
9
- logging.basicConfig(level=logging.DEBUG)
 
10
 
11
- def call_api(negative_prompt, positive_prompt, api_key):
12
- # API URL
13
- url = "https://model-5qe9kjp3.api.baseten.co/development/predict"
14
-
15
- # Headers including the provided API Key
16
- headers = {"Authorization": f"Api-Key {api_key}"}
17
-
18
- # JSON payload with the prompts
19
- payload = {
20
- 'workflow_values': {
21
- 'negative_prompt': negative_prompt,
22
- 'positive_prompt': positive_prompt
23
  }
24
- }
25
-
26
- # POST request to the API
27
- response = requests.post(url, headers=headers, json=payload)
28
-
29
- # Check if the request was successful
30
- if response.status_code == 200:
31
- data = response.json()['result'][0]['data']
32
-
33
- # Decode the base64 string to bytes
34
- image_data = base64.b64decode(data)
35
-
36
- # Convert bytes data to an image
37
- image = Image.open(BytesIO(image_data))
38
-
39
- # Save the image as PNG to a buffer
40
- buffer = BytesIO()
41
- image.save(buffer, format="PNG")
42
- buffer.seek(0)
43
-
44
- # Return the buffer content to display it directly in the interface
45
- return buffer
46
- else:
47
- return f"Failed to fetch the image. Status Code: {response.status_code}, Error: {response.text}"
48
 
49
- # Define Gradio interface
50
- interface = gr.Interface(
51
- fn=call_api,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  inputs=[
53
- gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt here..."),
54
- gr.Textbox(label="Positive Prompt", placeholder="Enter positive prompt here..."),
55
- gr.Textbox(label="API Key", placeholder="Enter your API key here..."),
 
56
  ],
57
- outputs="image",
58
- title="Image Generation API Interface",
59
- description="Enter the negative and positive prompts to generate an image."
 
 
 
60
  )
61
 
62
  # Launch the interface
63
- interface.launch()
64
-
 
1
  import gradio as gr
2
  import requests
3
  import base64
 
4
  from PIL import Image
5
+ import io
6
+ import json
7
 
8
+ def call_api_and_generate_image(api_key, negative_prompt, positive_prompt, controlnet_image):
9
+ if not api_key:
10
+ return None, "Error: API key is required. Please provide a valid API key."
11
 
12
+ resp = requests.post(
13
+ "https://model-4w561emq.api.baseten.co/development/predict",
14
+ headers={"Authorization": f"Api-Key {api_key}"},
15
+ json={
16
+ 'workflow_values': {
17
+ 'negative_prompt': negative_prompt,
18
+ 'positive_prompt': positive_prompt,
19
+ 'controlnet_image': controlnet_image
20
+ }
 
 
 
21
  }
22
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ if resp.status_code != 200:
25
+ return None, f"Error: API call failed with status code {resp.status_code}, message: {resp.text}"
26
+
27
+ result = resp.json()
28
+ base64_data = result['result'][0]['data']
29
+ image_data = base64.b64decode(base64_data)
30
+ image = Image.open(io.BytesIO(image_data))
31
+ return image, json.dumps(result, indent=2)
32
+
33
+ def generate_image(api_key, negative_prompt, positive_prompt, controlnet_image):
34
+ try:
35
+ return call_api_and_generate_image(api_key, negative_prompt, positive_prompt, controlnet_image)
36
+ except Exception as e:
37
+ return None, f"Error: {str(e)}"
38
+
39
+ # Create the Gradio interface
40
+ iface = gr.Interface(
41
+ fn=generate_image,
42
  inputs=[
43
+ gr.Textbox(label="API Key", placeholder="Enter your API key here"),
44
+ gr.Textbox(label="Negative Prompt", value="blurry, text, low quality", placeholder="Enter negative prompt here..."),
45
+ gr.Textbox(label="Positive Prompt", value="An igloo on a snowy day, 4k, hd", placeholder="Enter positive prompt here..."),
46
+ gr.Textbox(label="ControlNet Image URL", value="https://storage.googleapis.com/logos-bucket-01/baseten_logo.png", placeholder="Enter ControlNet image URL here...")
47
  ],
48
+ outputs=[
49
+ gr.Image(type="pil", label="Generated Image"),
50
+ gr.Textbox(label="API Response JSON", lines=10)
51
+ ],
52
+ title="Image Generation with API",
53
+ description="Enter your API key and prompts to generate an image using the provided API."
54
  )
55
 
56
  # Launch the interface
57
+ iface.launch()