geeksiddhant commited on
Commit
f19c84d
·
verified ·
1 Parent(s): d727678

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import gradio as gr
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import os
7
+
8
+ # Define a function to decode the base64 image and save it as a file
9
+ def 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 filename
13
+ filename = f"output_image.{image_format}"
14
+ image.save(filename)
15
+ return filename
16
+
17
+ # Define the function that will call the API and handle the image
18
+ def query_api(negative_prompt, positive_prompt):
19
+ # Prepare the headers and the JSON body of the request
20
+ headers = {
21
+ "Authorization": "Api-Key eC33svnV.vbYFrAXMrBZrqb7ukIp073gVC2ikRkfJ"
22
+ }
23
+ json_data = {
24
+ 'workflow_values': {
25
+ 'negative_prompt': negative_prompt,
26
+ 'positive_prompt': positive_prompt
27
+ }
28
+ }
29
+
30
+ # Send the POST request
31
+ response = requests.post(
32
+ "https://model-6wgvjxvw.api.baseten.co/development/predict",
33
+ headers=headers,
34
+ json=json_data
35
+ )
36
+
37
+ # Process the response
38
+ 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 display
43
+ 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 image
47
+ image_path = save_image(base64_image, image_format)
48
+ return image_path
49
+ 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 interface
57
+ iface = 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 app
69
+ iface.launch()