Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# Get API key from environment variable
|
6 |
+
api_key = os.environ.get("NVCF_API_KEY")
|
7 |
+
|
8 |
+
if not api_key:
|
9 |
+
raise ValueError("Please set the NVCF_API_KEY environment variable.")
|
10 |
+
|
11 |
+
# API details
|
12 |
+
invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/89848fb8-549f-41bb-88cb-95d6597044a4"
|
13 |
+
fetch_url_format = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {api_key}",
|
16 |
+
"Accept": "application/json",
|
17 |
+
}
|
18 |
+
|
19 |
+
# Function to generate image using the API
|
20 |
+
def generate_image(prompt, negative_prompt, sampler, seed, guidance_scale, inference_steps):
|
21 |
+
payload = {
|
22 |
+
"prompt": prompt,
|
23 |
+
"negative_prompt": negative_prompt,
|
24 |
+
"sampler": sampler,
|
25 |
+
"seed": seed,
|
26 |
+
"guidance_scale": guidance_scale,
|
27 |
+
"inference_steps": inference_steps
|
28 |
+
}
|
29 |
+
|
30 |
+
session = requests.Session()
|
31 |
+
response = session.post(invoke_url, headers=headers, json=payload)
|
32 |
+
|
33 |
+
while response.status_code == 202:
|
34 |
+
request_id = response.headers.get("NVCF-REQID")
|
35 |
+
fetch_url = fetch_url_format + request_id
|
36 |
+
response = session.get(fetch_url, headers=headers)
|
37 |
+
|
38 |
+
response.raise_for_status()
|
39 |
+
response_body = response.json()
|
40 |
+
|
41 |
+
# Extract image URL from response
|
42 |
+
image_url = response_body.get("output").get("image_url")
|
43 |
+
return image_url
|
44 |
+
|
45 |
+
# Create Gradio interface
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=generate_image,
|
48 |
+
inputs=[
|
49 |
+
gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
|
50 |
+
gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image"),
|
51 |
+
gr.Dropdown(label="Sampler", choices=["DPM", "DDPM", "PLMS"], value="DPM"),
|
52 |
+
gr.Number(label="Seed", value=0),
|
53 |
+
gr.Slider(label="Guidance Scale", minimum=0, maximum=20, value=5),
|
54 |
+
gr.Slider(label="Inference Steps", minimum=1, maximum=50, value=25)
|
55 |
+
],
|
56 |
+
outputs=gr.Image(label="Generated Image")
|
57 |
+
)
|
58 |
+
|
59 |
+
# Launch the Gradio app
|
60 |
+
iface.launch()
|