Aarifkhan commited on
Commit
d6da1d1
1 Parent(s): 8deae54

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import io
4
+ import random
5
+ import os
6
+ from PIL import Image
7
+
8
+ # List of available models
9
+ list_models = [
10
+ "NSFW-GEN"
11
+ ]
12
+
13
+ # Function to generate images from text
14
+ def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7, seed=None):
15
+ API_URL = "https://api-inference.huggingface.co/models/MysteriousAI/NSFW-gen"
16
+ API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
17
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
18
+
19
+ base_payload = {
20
+ "inputs": prompt,
21
+ "is_negative": is_negative,
22
+ "steps": steps,
23
+ "cfg_scale": cfg_scale,
24
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
25
+ }
26
+
27
+ if image_style == "None style":
28
+ base_payload["inputs"] += ", 8k"
29
+ elif image_style == "Cinematic":
30
+ base_payload["inputs"] += ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko"
31
+ base_payload["is_negative"] += ", abstract, cartoon, stylized"
32
+ elif image_style == "Digital Art":
33
+ base_payload["inputs"] += ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star"
34
+ base_payload["is_negative"] += ", sharp , modern , bright"
35
+ elif image_style == "Portrait":
36
+ base_payload["inputs"] += ", soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)"
37
+
38
+ image_bytes = requests.post(API_URL, headers=headers, json=base_payload).content
39
+ image = Image.open(io.BytesIO(image_bytes))
40
+ return image
41
+
42
+ css = """
43
+ /* General Container Styles */
44
+ .gradio-container {
45
+ font-family: 'IBM Plex Sans', sans-serif;
46
+ max-width: 730px !important;
47
+ margin: auto;
48
+ padding-top: 1.5rem;
49
+ text-align: center; /* Center the content horizontally */
50
+ }
51
+ /* Button Styles */
52
+ .gr-button {
53
+ color: white;
54
+ background: #007bff; /* Use a primary color for the background */
55
+ white-space: nowrap;
56
+ border: none;
57
+ padding: 10px 20px;
58
+ border-radius: 8px;
59
+ cursor: pointer;
60
+ transition: background-color 0.3s, color 0.3s;
61
+ }
62
+ .gr-button:hover {
63
+ background-color: #0056b3; /* Darken the background color on hover */
64
+ }
65
+ /* Share Button Styles */
66
+ #share-btn-container {
67
+ padding: 0.5rem !important;
68
+ background-color: #007bff; /* Use a primary color for the background */
69
+ justify-content: center;
70
+ align-items: center;
71
+ border-radius: 9999px !important;
72
+ max-width: 13rem;
73
+ margin: 0 auto; /* Center the container horizontally */
74
+ transition: background-color 0.3s;
75
+ }
76
+ #share-btn-container:hover {
77
+ background-color: #0056b3; /* Darken the background color on hover */
78
+ }
79
+ #share-btn {
80
+ all: initial;
81
+ color: #ffffff;
82
+ font-weight: 600;
83
+ cursor: pointer;
84
+ font-family: 'IBM Plex Sans', sans-serif;
85
+ margin: 0.5rem !important;
86
+ padding: 0.5rem !important;
87
+ }
88
+ /* Other Styles */
89
+ #gallery {
90
+ min-height: 22rem;
91
+ margin: auto; /* Center the gallery horizontally */
92
+ border-bottom-right-radius: 0.5rem !important;
93
+ border-bottom-left-radius: 0.5rem !important;
94
+ }
95
+ /* Centered Container for the Image */
96
+ .image-container {
97
+ max-width: 100%; /* Set the maximum width for the container */
98
+ margin: auto; /* Center the container horizontally */
99
+ padding: 20px; /* Add padding for spacing */
100
+ border: 1px solid #ccc; /* Add a subtle border to the container */
101
+ border-radius: 10px;
102
+ overflow: hidden; /* Hide overflow if the image is larger */
103
+ max-height: 22rem; /* Set a maximum height for the container */
104
+ }
105
+ /* Set a fixed size for the image */
106
+ .image-container img {
107
+ max-width: 100%; /* Ensure the image fills the container */
108
+ height: auto; /* Maintain aspect ratio */
109
+ max-height: 100%; /* Set a maximum height for the image */
110
+ border-radius: 10px;
111
+ box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
112
+ }
113
+ """
114
+
115
+ # Creating Gradio interface
116
+ with gr.Blocks(css=css) as demo:
117
+
118
+ with gr.Row():
119
+ with gr.Column():
120
+ gr.Markdown("<h1>PixelCraft</h1>")
121
+ current_model = gr.Dropdown(label="Select Model", choices=list_models, value=list_models[0])
122
+ text_prompt = gr.Textbox(label="Enter Prompt", placeholder="Example: a cute dog", lines=2)
123
+ generate_button = gr.Button("Generate Image", variant='primary')
124
+
125
+ with gr.Column():
126
+ gr.Markdown("<h4>Advanced Settings</h4>")
127
+ with gr.Accordion("Advanced Customizations", open=False):
128
+ negative_prompt = gr.Textbox(label="Negative Prompt (Optional)", placeholder="Example: blurry, unfocused", lines=2)
129
+ image_style = gr.Dropdown(label="Select Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style")
130
+ # Add more options if needed
131
+
132
+ with gr.Row():
133
+ image_output = gr.Image(type="pil", label="Output Image")
134
+
135
+ generate_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
136
+
137
+ # Launch the app
138
+ demo.launch()