Spaces:
Build error
Build error
ABDALLALSWAITI
commited on
Commit
•
a0c2904
1
Parent(s):
6c28935
Upload folder using huggingface_hub
Browse files- .github/workflows/update_space.yml +28 -0
- DAVINCIDIFFtextgardio.py +178 -0
- README.md +1 -7
- requirements.txt +1 -0
.github/workflows/update_space.yml
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Run Python script
|
2 |
+
|
3 |
+
on:
|
4 |
+
push:
|
5 |
+
branches:
|
6 |
+
- main
|
7 |
+
|
8 |
+
jobs:
|
9 |
+
build:
|
10 |
+
runs-on: ubuntu-latest
|
11 |
+
|
12 |
+
steps:
|
13 |
+
- name: Checkout
|
14 |
+
uses: actions/checkout@v2
|
15 |
+
|
16 |
+
- name: Set up Python
|
17 |
+
uses: actions/setup-python@v2
|
18 |
+
with:
|
19 |
+
python-version: '3.9'
|
20 |
+
|
21 |
+
- name: Install Gradio
|
22 |
+
run: python -m pip install gradio
|
23 |
+
|
24 |
+
- name: Log in to Hugging Face
|
25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
26 |
+
|
27 |
+
- name: Deploy to Spaces
|
28 |
+
run: gradio deploy
|
DAVINCIDIFFtextgardio.py
ADDED
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import StableDiffusionXLPipeline, AutoencoderTiny
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import google.generativeai as genai
|
6 |
+
import gradio as gr
|
7 |
+
import random
|
8 |
+
|
9 |
+
art_styles = [
|
10 |
+
"Impressionism", "Surrealism", "Cubism", "Abstract", "Realism", "Expressionism",
|
11 |
+
"Pop Art", "Futurism", "Minimalism", "Conceptual Art", "Baroque", "Renaissance",
|
12 |
+
"Gothic", "Neoclassicism", "Romanticism", "Art Nouveau", "Art Deco", "Post-Impressionism"
|
13 |
+
]
|
14 |
+
|
15 |
+
artists = [
|
16 |
+
"Pablo Picasso", "Vincent van Gogh", "Leonardo da Vinci", "Claude Monet", "Salvador Dalí",
|
17 |
+
"Frida Kahlo", "Rembrandt", "Michelangelo", "Andy Warhol", "Jackson Pollock",
|
18 |
+
"Georgia O'Keeffe", "Edvard Munch", "Henri Matisse", "Paul Cézanne", "Gustav Klimt",
|
19 |
+
"Caravaggio", "Jean-Michel Basquiat", "Raphael", "Auguste Rodin", "Yayoi Kusama"
|
20 |
+
]
|
21 |
+
|
22 |
+
light_effects = [
|
23 |
+
"soft lighting", "dramatic lighting", "rim lighting", "ambient lighting", "studio lighting",
|
24 |
+
"natural light", "backlighting", "side lighting", "low-key lighting", "high-key lighting"
|
25 |
+
]
|
26 |
+
|
27 |
+
depth_effects = [
|
28 |
+
"shallow depth of field", "deep depth of field", "foreground focus", "background blur",
|
29 |
+
"bokeh effect", "layered depth", "aerial perspective", "overlapping elements", "linear perspective",
|
30 |
+
"vanishing point"
|
31 |
+
]
|
32 |
+
|
33 |
+
# Function to save the Gemini API key
|
34 |
+
def save_gemini_api_key(api_key):
|
35 |
+
with open("gemini_api_key.json", "w") as f:
|
36 |
+
json.dump({"api_key": api_key}, f)
|
37 |
+
|
38 |
+
# Function to load the Gemini API key
|
39 |
+
def load_gemini_api_key():
|
40 |
+
try:
|
41 |
+
with open("gemini_api_key.json", "r") as f:
|
42 |
+
data = json.load(f)
|
43 |
+
return data.get("api_key", "")
|
44 |
+
except FileNotFoundError:
|
45 |
+
return ""
|
46 |
+
|
47 |
+
# Function to delete the Gemini API key
|
48 |
+
def delete_gemini_api_key():
|
49 |
+
try:
|
50 |
+
if os.path.exists("gemini_api_key.json"):
|
51 |
+
os.remove("gemini_api_key.json")
|
52 |
+
except Exception as e:
|
53 |
+
print(f"Error deleting Gemini API key: {e}")
|
54 |
+
|
55 |
+
# Function to enhance prompt using Gemini's API with specific instructions
|
56 |
+
def enhance_prompt_with_gemini(prompt, api_key):
|
57 |
+
try:
|
58 |
+
if api_key:
|
59 |
+
genai.configure(api_key=api_key)
|
60 |
+
|
61 |
+
# Construct the specific instruction prompt
|
62 |
+
instruction = f"You are Professor of creating a prompt for Stable Diffusion to generate an image. Write the phrases for art styles, art movements, artists names, light and depth effects for this {prompt}. Make this prompt the best ever, only response with prompt itself in one paragraph."
|
63 |
+
|
64 |
+
# Generate content using Gemini API with the instruction prompt
|
65 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
66 |
+
response = model.generate_content(instruction)
|
67 |
+
enhanced_prompt = response.text.strip()
|
68 |
+
return enhanced_prompt
|
69 |
+
else:
|
70 |
+
return prompt
|
71 |
+
except Exception as e:
|
72 |
+
print(f"Failed to enhance prompt with Gemini API: {e}")
|
73 |
+
return prompt # Fallback to original prompt on error
|
74 |
+
|
75 |
+
# Function to generate images based on user input
|
76 |
+
def generate_image(prompt, negative_prompt, cfg_scale, steps, width, height, seed, art_style, artist, light_effect, depth_effect, api_key):
|
77 |
+
queue = []
|
78 |
+
additional_prompts = f"{art_style}, {artist}, {light_effect}, {depth_effect}"
|
79 |
+
full_prompt = f"{prompt}, {additional_prompts}"
|
80 |
+
enhanced_prompt = enhance_prompt_with_gemini(full_prompt, api_key)
|
81 |
+
|
82 |
+
# Generate a random seed if not provided
|
83 |
+
if seed is None or seed == "":
|
84 |
+
seed = random.randint(0, 2**32 - 1)
|
85 |
+
|
86 |
+
print("\nStarting image generation with the following parameters:")
|
87 |
+
print(f"Enhanced Prompt: {enhanced_prompt}")
|
88 |
+
print(f"Negative Prompt: {negative_prompt}")
|
89 |
+
print(f"CFG Scale: {cfg_scale}")
|
90 |
+
print(f"Steps: {steps}")
|
91 |
+
print(f"Width: {width}")
|
92 |
+
print(f"Height: {height}")
|
93 |
+
print(f"Seed: {seed}")
|
94 |
+
|
95 |
+
queue.append({
|
96 |
+
'prompt': enhanced_prompt,
|
97 |
+
'negative_prompt': negative_prompt,
|
98 |
+
'cfg_scale': cfg_scale,
|
99 |
+
'steps': steps,
|
100 |
+
'width': width,
|
101 |
+
'height': height,
|
102 |
+
'seed': seed,
|
103 |
+
})
|
104 |
+
|
105 |
+
vae = AutoencoderTiny.from_pretrained(
|
106 |
+
'madebyollin/taesdxl',
|
107 |
+
use_safetensors=True,
|
108 |
+
torch_dtype=torch.float16,
|
109 |
+
).to('cpu')
|
110 |
+
|
111 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
112 |
+
'ABDALLALSWAITI/DAVINCI-DIFF',
|
113 |
+
torch_dtype=torch.float16,
|
114 |
+
use_safetensors=True,
|
115 |
+
vae=vae
|
116 |
+
).to('cuda')
|
117 |
+
|
118 |
+
generator = torch.manual_seed(seed)
|
119 |
+
|
120 |
+
image = pipe(
|
121 |
+
prompt=enhanced_prompt,
|
122 |
+
negative_prompt=negative_prompt,
|
123 |
+
height=height,
|
124 |
+
width=width,
|
125 |
+
num_inference_steps=steps,
|
126 |
+
guidance_scale=cfg_scale,
|
127 |
+
generator=generator,
|
128 |
+
output_type="pil",
|
129 |
+
).images[0]
|
130 |
+
|
131 |
+
return image
|
132 |
+
|
133 |
+
# Gradio Blocks interface
|
134 |
+
api_key = load_gemini_api_key()
|
135 |
+
|
136 |
+
def on_submit(prompt, negative_prompt, cfg_scale, steps, width, height, seed, art_style, artist, light_effect, depth_effect, api_key):
|
137 |
+
return generate_image(prompt, negative_prompt, cfg_scale, steps, width, height, seed, art_style, artist, light_effect, depth_effect, api_key)
|
138 |
+
|
139 |
+
with gr.Blocks() as interface:
|
140 |
+
gr.Markdown("# Image Generation with Stable Diffusion")
|
141 |
+
|
142 |
+
with gr.Row():
|
143 |
+
prompt = gr.Textbox(label="Prompt", value="3/4 shot, candid photograph of a beautiful 30 year old redhead woman with messy dark hair, peacefully sleeping in her bed, night, dark, light from window, dark shadows, masterpiece, uhd, moody")
|
144 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value="")
|
145 |
+
|
146 |
+
with gr.Row():
|
147 |
+
cfg_scale = gr.Number(label="CFG Scale", value=7.5)
|
148 |
+
steps = gr.Number(label="Steps", value=50)
|
149 |
+
|
150 |
+
with gr.Row():
|
151 |
+
width = gr.Number(label="Image Width", value=1024)
|
152 |
+
height = gr.Number(label="Image Height", value=1024)
|
153 |
+
|
154 |
+
seed = gr.Number(label="Seed (leave empty for random seed)", value=None)
|
155 |
+
|
156 |
+
with gr.Row():
|
157 |
+
art_style = gr.Dropdown(label="Art Style", choices=art_styles, value="Impressionism")
|
158 |
+
artist = gr.Dropdown(label="Artist", choices=artists, value="Vincent van Gogh")
|
159 |
+
|
160 |
+
with gr.Row():
|
161 |
+
light_effect = gr.Dropdown(label="Light Effect", choices=light_effects, value="soft lighting")
|
162 |
+
depth_effect = gr.Dropdown(label="Depth Effect", choices=depth_effects, value="shallow depth of field")
|
163 |
+
|
164 |
+
api_key = gr.Textbox(label="Gemini API Key", type="password", placeholder="Enter Gemini API Key (optional)")
|
165 |
+
|
166 |
+
generate_btn = gr.Button("Generate Image")
|
167 |
+
output_image = gr.Image(label="Generated Image")
|
168 |
+
|
169 |
+
generate_btn.click(
|
170 |
+
on_submit,
|
171 |
+
inputs=[prompt, negative_prompt, cfg_scale, steps, width, height, seed, art_style, artist, light_effect, depth_effect, api_key],
|
172 |
+
outputs=output_image
|
173 |
+
)
|
174 |
+
|
175 |
+
interface.launch(share=True)
|
176 |
+
|
177 |
+
def on_session_end(session):
|
178 |
+
delete_gemini_api_key()
|
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
title: Davinci
|
3 |
-
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.38.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Davinci
|
3 |
+
app_file: DAVINCIDIFFtextgardio.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.38.1
|
|
|
|
|
6 |
---
|
|
|
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
torch, diffusers, google.generativeai, gradio, random
|