nevreal commited on
Commit
67d278f
1 Parent(s): c7685f0

Create app.py

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