geeksiddhant commited on
Commit
dbadb9f
·
verified ·
1 Parent(s): 39f2ae5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ def call_api(negative_prompt, positive_prompt, api_key):
8
+ # API URL
9
+ url = "https://model-5qe9kjp3.api.baseten.co/development/predict"
10
+
11
+ # Headers including the provided API Key
12
+ headers = {"Authorization": f"Api-Key {api_key}"}
13
+
14
+ # JSON payload with the prompts
15
+ payload = {
16
+ 'workflow_values': {
17
+ 'negative_prompt': negative_prompt,
18
+ 'positive_prompt': positive_prompt
19
+ }
20
+ }
21
+
22
+ # POST request to the API
23
+ response = requests.post(url, headers=headers, json=payload)
24
+
25
+ # Check if the request was successful
26
+ if response.status_code == 200:
27
+ data = response.json()['result'][0]['data']
28
+ format = response.json()['result'][0]['format']
29
+
30
+ # Decode the base64 string to bytes
31
+ image_data = base64.b64decode(data)
32
+
33
+ # Convert bytes data to an image
34
+ image = Image.open(BytesIO(image_data))
35
+
36
+ return image
37
+ else:
38
+ return "Failed to fetch the image. Please check the API key and inputs."
39
+
40
+ # Define Gradio interface
41
+ interface = gr.Interface(
42
+ fn=call_api,
43
+ inputs=[
44
+ gr.Textbox(label="Negative Prompt", placeholder="Enter negative prompt here..."),
45
+ gr.Textbox(label="Positive Prompt", placeholder="Enter positive prompt here..."),
46
+ gr.Textbox(label="API Key", placeholder="Enter your API key here..."),
47
+ ],
48
+ outputs="image",
49
+ title="Image Generation API Interface",
50
+ description="Enter the negative and positive prompts to generate an image."
51
+ )
52
+
53
+ # Launch the interface
54
+ interface.launch()