nevreal commited on
Commit
050a39a
1 Parent(s): a9d15f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -34
app.py CHANGED
@@ -1,44 +1,48 @@
1
  import gradio as gr
2
  import requests
3
- import json
 
4
  import os
5
 
6
- # Hugging Face Inference API setup
7
- API_URL = "https://api-inference.huggingface.co/models/Benevolent/PonyDiffusionV10"
8
- HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Get token from environment variable
9
- headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
10
 
11
- def query(payload):
12
- response = requests.post(API_URL, headers=headers, json=payload)
13
- return json.loads(response.content)
14
 
15
- # Function to call the HF Inference API
16
- def generate_image(prompt):
17
- payload = {
18
- "inputs": prompt,
19
- }
20
- response = query(payload)
21
- if 'error' in response:
22
- return f"Error: {response['error']}"
23
- # Check for the generated image
24
- if isinstance(response, list) and len(response) > 0 and "generated_image" in response[0]:
25
- return response[0]["generated_image"] # Base64 encoded image
26
- return "No image returned from model"
27
 
28
- # Gradio Blocks Web UI
29
- with gr.Blocks() as demo:
30
- with gr.Row():
31
- gr.Markdown("# PonyDiffusion V10 Text-to-Image Generator")
32
-
33
- with gr.Row():
34
- prompt = gr.Textbox(label="Enter a prompt for the image")
35
 
36
- with gr.Row():
37
- generate_button = gr.Button("Generate Image")
38
- output_image = gr.Image()
 
 
 
 
 
 
 
 
 
 
39
 
40
- # When generate_button is clicked, call generate_image
41
- generate_button.click(fn=generate_image, inputs=prompt, outputs=output_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- # Launch the app
44
- demo.launch()
 
 
1
  import gradio as gr
2
  import requests
3
+ from PIL import Image
4
+ from io import BytesIO
5
  import os
6
 
 
 
 
 
7
 
 
 
 
8
 
9
+ # Load API Token from environment variable
10
+ API_TOKEN = os.getenv("HF_API_TOKEN") # Ensure you've set this environment variable
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Hugging Face Inference API URL
13
+ API_URL = "https://api-inference.huggingface.co/models/Benevolent/PonyDiffusionV10"
 
 
 
 
 
14
 
15
+ # Function to call Hugging Face API and get the generated image
16
+ def generate_image(prompt):
17
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
18
+ data = {"inputs": prompt}
19
+
20
+ response = requests.post(API_URL, headers=headers, json=data)
21
+
22
+ if response.status_code == 200:
23
+ image_bytes = BytesIO(response.content)
24
+ image = Image.open(image_bytes)
25
+ return image
26
+ else:
27
+ return f"Error: {response.status_code}, {response.text}"
28
 
29
+ # Create Gradio interface
30
+ def create_ui():
31
+ with gr.Blocks() as ui:
32
+ gr.Markdown("## PonyDiffusionV10 - Text to Image Generator")
33
+
34
+ with gr.Row():
35
+ prompt_input = gr.Textbox(label="Enter a Prompt", placeholder="Describe the image you want to generate", lines=3)
36
+ generate_button = gr.Button("Generate Image")
37
+
38
+ with gr.Row():
39
+ output_image = gr.Image(label="Generated Image")
40
+
41
+ # Link the button to the function
42
+ generate_button.click(fn=generate_image, inputs=prompt_input, outputs=output_image)
43
+
44
+ return ui
45
 
46
+ # Run the interface
47
+ if __name__ == "__main__":
48
+ create_ui().launch()