amirkhanbloch commited on
Commit
3e7fe19
·
verified ·
1 Parent(s): e52b864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -1,30 +1,34 @@
1
  import gradio as gr
2
- import torch
3
- from diffusers import StableDiffusionPipeline
 
 
 
 
4
 
5
- # Load the pipeline outside the function to avoid reloading it for each request
6
- pipe = StableDiffusionPipeline.from_pretrained(
7
- "CompVis/stable-diffusion-v1-4",
8
- revision="fp16",
9
- torch_dtype=torch.float16
10
- ).to("cuda")
11
 
12
- # Check if the model is on GPU
13
- print("Is the model on GPU?", next(pipe.parameters()).is_cuda)
 
 
14
 
15
- # Define a function to generate the image
16
- def generate_image(prompt):
17
- image = pipe(prompt).images[0]
 
18
  return image
19
 
20
- # Gradio interface: Text input for the prompt, and image output
21
- interface = gr.Interface(
22
- fn=generate_image,
23
- inputs="text",
24
- outputs="image",
25
- title="Stable Diffusion Image Generator",
26
- description="Generate an image based on your text prompt using Stable Diffusion."
27
  )
28
 
29
  # Launch the Gradio app
30
- interface.launch()
 
1
  import gradio as gr
2
+ import requests
3
+ import io
4
+ from PIL import Image
5
+ import os
6
+ from dotenv import load_dotenv
7
+ load_dotenv()
8
 
9
+ # Define the API URL and headers
10
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
11
+ headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
 
 
 
12
 
13
+ # Define the query function
14
+ def query(payload):
15
+ response = requests.post(API_URL, headers=headers, json=payload)
16
+ return response.content
17
 
18
+ # Define the function to generate the image
19
+ def generate_image(input_text):
20
+ image_bytes = query({"inputs": input_text})
21
+ image = Image.open(io.BytesIO(image_bytes))
22
  return image
23
 
24
+ # Create Gradio interface
25
+ iface = gr.Interface(
26
+ fn=generate_image,
27
+ inputs=gr.inputs.Textbox(label="Input Text", placeholder="Enter your description here..."),
28
+ outputs=gr.outputs.Image(type="pil", label="Generated Image"),
29
+ title="Image Generation with FLUX",
30
+ description="Enter a description to generate an image."
31
  )
32
 
33
  # Launch the Gradio app
34
+ iface.launch()