Scalino84 commited on
Commit
790ba0f
·
verified ·
1 Parent(s): 008080a

Upload /home/user/app/bck/app_von_hugging.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. home/user/app/bck/app_von_hugging.py +161 -0
home/user/app/bck/app_von_hugging.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import os
5
+ from huggingface_hub import InferenceClient
6
+ from diffusers import StableDiffusionPipeline
7
+ from diffusers.schedulers import DPMSolverMultistepScheduler
8
+ import torch
9
+
10
+ # Token aus Umgebungsvariablen abrufen
11
+ token = os.getenv("HF_TOKEN")
12
+
13
+ # InferenceClient initialisieren
14
+ client = InferenceClient(
15
+ model="black-forest-labs/FLUX.1-dev",
16
+ token=token
17
+ )
18
+
19
+ device = "cuda" if torch.cuda.is_available() else "cpu"
20
+ model_id = "stabilityai/stable-diffusion-2-1" # Basismodell-ID
21
+
22
+ # Lade das Basismodell
23
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
24
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
25
+ pipe = pipe.to(device)
26
+
27
+ MAX_SEED = np.iinfo(np.int32).max
28
+ MAX_IMAGE_SIZE = 1024
29
+
30
+ def infer(
31
+ prompt,
32
+ negative_prompt,
33
+ seed,
34
+ randomize_seed,
35
+ width,
36
+ height,
37
+ guidance_scale,
38
+ num_inference_steps,
39
+ progress=gr.Progress(track_tqdm=True),
40
+ ):
41
+ if randomize_seed:
42
+ seed = random.randint(0, MAX_SEED)
43
+
44
+ generator = torch.Generator(device=device).manual_seed(seed)
45
+
46
+ try:
47
+ image = pipe(
48
+ prompt=prompt,
49
+ negative_prompt=negative_prompt,
50
+ guidance_scale=guidance_scale,
51
+ num_inference_steps=num_inference_steps,
52
+ width=width,
53
+ height=height,
54
+ generator=generator,
55
+ ).images[0]
56
+ except Exception as e:
57
+ return f"Fehler bei der Bildgenerierung: {str(e)}", seed
58
+
59
+ return image, seed
60
+
61
+ examples = [
62
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
63
+ "An astronaut riding a green horse",
64
+ "A delicious ceviche cheesecake slice",
65
+ ]
66
+
67
+ css = """
68
+ #col-container {
69
+ margin: 0 auto;
70
+ max-width: 640px;
71
+ }
72
+ """
73
+
74
+ with gr.Blocks(css=css) as demo:
75
+ with gr.Column(elem_id="col-container"):
76
+ gr.Markdown(" # Text-to-Image Gradio Template")
77
+
78
+ with gr.Row():
79
+ prompt = gr.Textbox(
80
+ label="Prompt",
81
+ show_label=False,
82
+ max_lines=1,
83
+ placeholder="Enter your prompt",
84
+ container=False,
85
+ )
86
+
87
+ run_button = gr.Button("Run", scale=0, variant="primary")
88
+
89
+ result = gr.Image(label="Result", show_label=False)
90
+
91
+ with gr.Accordion("Advanced Settings", open=False):
92
+ negative_prompt = gr.Textbox(
93
+ label="Negative prompt",
94
+ max_lines=1,
95
+ placeholder="Enter a negative prompt",
96
+ visible=True,
97
+ )
98
+
99
+ seed = gr.Slider(
100
+ label="Seed",
101
+ minimum=0,
102
+ maximum=MAX_SEED,
103
+ step=1,
104
+ value=0,
105
+ )
106
+
107
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
108
+
109
+ with gr.Row():
110
+ width = gr.Slider(
111
+ label="Width",
112
+ minimum=256,
113
+ maximum=MAX_IMAGE_SIZE,
114
+ step=32,
115
+ value=512, # Replace with defaults that work for your model
116
+ )
117
+
118
+ height = gr.Slider(
119
+ label="Height",
120
+ minimum=256,
121
+ maximum=MAX_IMAGE_SIZE,
122
+ step=32,
123
+ value=512, # Replace with defaults that work for your model
124
+ )
125
+
126
+ with gr.Row():
127
+ guidance_scale = gr.Slider(
128
+ label="Guidance scale",
129
+ minimum=0.0,
130
+ maximum=20.0,
131
+ step=0.1,
132
+ value=7.5, # Replace with defaults that work for your model
133
+ )
134
+
135
+ num_inference_steps = gr.Slider(
136
+ label="Number of inference steps",
137
+ minimum=1,
138
+ maximum=100,
139
+ step=1,
140
+ value=50, # Replace with defaults that work for your model
141
+ )
142
+
143
+ gr.Examples(examples=examples, inputs=[prompt])
144
+
145
+ run_button.click(
146
+ fn=infer,
147
+ inputs=[
148
+ prompt,
149
+ negative_prompt,
150
+ seed,
151
+ randomize_seed,
152
+ width,
153
+ height,
154
+ guidance_scale,
155
+ num_inference_steps,
156
+ ],
157
+ outputs=[result, seed],
158
+ )
159
+
160
+ if __name__ == "__main__":
161
+ demo.launch(share=True)