Narenameme commited on
Commit
06df236
·
verified ·
1 Parent(s): ee55365
Files changed (2) hide show
  1. app.py +18 -12
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,19 +1,25 @@
1
  import gradio as gr
2
- from diffusers import DiffusionPipeline
3
- import torch
4
 
5
- # Initialize the model pipeline
6
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",
7
- torch_dtype=torch.float16,
8
- use_safetensors=True,
9
- variant="fp16")
10
 
11
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
12
-
13
- # Define the inference function
14
  def generate_image(prompt):
15
- image = pipe(prompt).images[0]
16
- return image
 
 
 
 
 
 
 
 
 
17
 
18
  # Create the Gradio interface
19
  iface = gr.Interface(fn=generate_image, inputs="text", outputs="image", live=True)
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
 
5
+ api_key = os.getenv("HF_API_KEY")
6
+ # Define the Hugging Face API endpoint and your API key
7
+ api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
8
+ headers = {"Authorization": f"Bearer {api_key}"}
 
9
 
10
+ # Define the inference function using the Hugging Face API
 
 
11
  def generate_image(prompt):
12
+ payload = {
13
+ "inputs": prompt
14
+ }
15
+ response = requests.post(api_url, headers=headers, json=payload)
16
+
17
+ if response.status_code == 200:
18
+ # If the response is successful, return the image
19
+ image = response.content
20
+ return image
21
+ else:
22
+ return "Error: " + response.text
23
 
24
  # Create the Gradio interface
25
  iface = gr.Interface(fn=generate_image, inputs="text", outputs="image", live=True)
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  diffusers
2
  torch
3
  gradio
 
 
 
1
  diffusers
2
  torch
3
  gradio
4
+ accelerate
5
+ python-dotenv