Scalino84 commited on
Commit
f5bd834
·
1 Parent(s): 6455ecf

Add application file

Browse files
Files changed (1) hide show
  1. app.py +41 -66
app.py CHANGED
@@ -1,73 +1,48 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
  import torch
4
- from huggingface_hub import HfFolder
5
-
6
- def generate_image(prompt, guidance_scale, num_steps, lora_scale):
7
- # Lade das Base Model
8
- pipe = StableDiffusionPipeline.from_pretrained(
9
- "runwayml/stable-diffusion-v1-5",
10
- torch_dtype=torch.float16
11
- ).to("cuda")
12
-
13
- # Lade dein LoRA
14
- pipe.load_lora_weights(
15
- "Scalino84/my-flux-face-v2",
16
- weight_name="flux_train_replicate.safetensors"
17
- )
18
-
19
- # Generiere das Bild
20
- image = pipe(
21
- prompt=prompt,
22
- num_inference_steps=num_steps,
23
- guidance_scale=guidance_scale,
24
- cross_attention_kwargs={"scale": lora_scale}
25
- ).images[0]
26
-
27
- return image
28
 
29
- # Erstelle das Gradio Interface
30
- with gr.Blocks() as demo:
31
- gr.Markdown("# Flux Face Generator")
32
-
33
- with gr.Row():
34
- with gr.Column():
35
- prompt = gr.Textbox(
36
- label="Prompt",
37
- value="a photo of xyz person, professional headshot",
38
- lines=3
39
- )
40
- guidance = gr.Slider(
41
- label="Guidance Scale",
42
- minimum=1,
43
- maximum=20,
44
- value=7.5,
45
- step=0.5
46
- )
47
- steps = gr.Slider(
48
- label="Inference Steps",
49
- minimum=20,
50
- maximum=100,
51
- value=30,
52
- step=1
53
- )
54
- lora_scale = gr.Slider(
55
- label="LoRA Scale",
56
- minimum=0.1,
57
- maximum=1.0,
58
- value=0.8,
59
- step=0.1
60
- )
61
- generate = gr.Button("Generate Image")
62
 
63
- with gr.Column():
64
- output = gr.Image(label="Generated Image")
 
 
 
 
 
 
65
 
66
- generate.click(
67
- fn=generate_image,
68
- inputs=[prompt, guidance, steps, lora_scale],
69
- outputs=output
70
- )
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- demo.launch()
 
73
 
 
1
  import gradio as gr
 
2
  import torch
3
+ from diffusers import StableDiffusionPipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ def generate_image(prompt, guidance_scale=7.5, num_steps=30, lora_scale=0.8):
6
+ try:
7
+ # Initialize the pipeline
8
+ pipe = StableDiffusionPipeline.from_pretrained(
9
+ "runwayml/stable-diffusion-v1-5",
10
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
11
+ )
12
+
13
+ # Load LoRA weights
14
+ pipe.load_lora_weights("Scalino84/my-flux-face-v2")
15
+
16
+ if torch.cuda.is_available():
17
+ pipe = pipe.to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Generate image
20
+ image = pipe(
21
+ prompt=prompt,
22
+ num_inference_steps=int(num_steps),
23
+ guidance_scale=float(guidance_scale),
24
+ ).images[0]
25
+
26
+ return image
27
 
28
+ except Exception as e:
29
+ print(f"Error: {str(e)}")
30
+ raise gr.Error(str(e))
31
+
32
+ # Create Gradio interface
33
+ interface = gr.Interface(
34
+ fn=generate_image,
35
+ inputs=[
36
+ gr.Textbox(label="Prompt", value="a photo of xyz person, professional headshot"),
37
+ gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale"),
38
+ gr.Slider(minimum=20, maximum=100, value=30, label="Number of Steps"),
39
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.8, label="LoRA Scale")
40
+ ],
41
+ outputs=gr.Image(label="Generated Image"),
42
+ title="Flux Face Generator",
43
+ description="Generate images using your custom LoRA model"
44
+ )
45
 
46
+ # Launch the interface
47
+ interface.launch()
48