Alibrown commited on
Commit
fc4ca24
·
verified ·
1 Parent(s): fca653e

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -123
app.py DELETED
@@ -1,123 +0,0 @@
1
- import os
2
- import random
3
- import numpy as np
4
- import torch
5
- import gradio as gr
6
- from diffusers import StableDiffusionPipeline
7
- import paramiko
8
- from huggingface_hub import login
9
-
10
- # Hugging Face Token
11
- HF_TOKEN = os.getenv('HF_TOKEN', '').strip()
12
- if not HF_TOKEN:
13
- raise ValueError("HUGGING_TOKEN is not set. Please set the token as an environment variable.")
14
-
15
- # Hugging Face Login
16
- login(token=HF_TOKEN)
17
-
18
- # Konfiguration
19
- STORAGE_DOMAIN = os.getenv('STORAGE_DOMAIN', '').strip() # SFTP Server Domain
20
- STORAGE_USER = os.getenv('STORAGE_USER', '').strip() # SFTP User
21
- STORAGE_PSWD = os.getenv('STORAGE_PSWD', '').strip() # SFTP Passwort
22
- STORAGE_PORT = int(os.getenv('STORAGE_PORT', '22').strip()) # SFTP Port
23
- STORAGE_SECRET = os.getenv('STORAGE_SECRET', '').strip() # Secret Token
24
-
25
- # Modell-Optionen - können angepasst werden
26
- MODEL_REPO = os.getenv('MODEL_REPO', 'stabilityai/stable-diffusion-2-1-base') # Standard-Modell
27
- TORCH_DTYPE = os.getenv('TORCH_DTYPE', 'float16') # Standard-Präzision
28
-
29
- # Modell laden
30
- device = "cuda" if torch.cuda.is_available() else "cpu"
31
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
32
-
33
- try:
34
- pipe = StableDiffusionPipeline.from_pretrained(
35
- MODEL_REPO,
36
- torch_dtype=torch_dtype
37
- ).to(device)
38
- except Exception as e:
39
- raise RuntimeError(f"Failed to load the model. Ensure the token has access to the repo. Error: {e}")
40
-
41
-
42
-
43
- try:
44
- pipe = StableDiffusionPipeline.from_pretrained(
45
- MODEL_REPO,
46
- torch_dtype=torch_dtype
47
- ).to(device)
48
- except Exception as e:
49
- raise RuntimeError(f"Failed to load the model. Ensure the token has access to the repo. Error: {e}")
50
-
51
- # Maximalwerte
52
- MAX_SEED = np.iinfo(np.int32).max
53
- MAX_IMAGE_SIZE = 1344
54
-
55
- # SFTP-Funktion
56
- def upload_to_sftp(local_file, remote_path):
57
- """Versucht, eine Datei auf einen SFTP-Server hochzuladen."""
58
- try:
59
- transport = paramiko.Transport((STORAGE_DOMAIN, STORAGE_PORT))
60
- transport.connect(username=STORAGE_USER, password=STORAGE_PSWD)
61
- sftp = paramiko.SFTPClient.from_transport(transport)
62
- sftp.put(local_file, remote_path)
63
- sftp.close()
64
- transport.close()
65
- return True
66
- except Exception as e:
67
- return f"Error during SFTP upload: {e}"
68
-
69
- # Inferenz-Funktion
70
- def infer(prompt, width, height, guidance_scale, num_inference_steps, seed, randomize_seed):
71
- """Generiert ein Bild basierend auf dem Eingabe-Prompt und lädt es auf einen SFTP-Server hoch."""
72
- if randomize_seed:
73
- seed = random.randint(0, MAX_SEED)
74
-
75
- generator = torch.manual_seed(seed)
76
- image = pipe(
77
- prompt,
78
- guidance_scale=guidance_scale,
79
- num_inference_steps=num_inference_steps,
80
- width=width,
81
- height=height,
82
- generator=generator
83
- ).images[0]
84
-
85
- # Speichere Bild lokal
86
- local_file = f"/tmp/generated_image_{seed}.png"
87
- image.save(local_file)
88
-
89
- # Hochladen zu SFTP
90
- remote_path = f"/uploads/generated_image_{seed}.png"
91
- upload_result = upload_to_sftp(local_file, remote_path)
92
-
93
- # Entferne das lokale Bild, wenn der Upload erfolgreich war
94
- if upload_result == True:
95
- os.remove(local_file)
96
- return f"Image successfully uploaded to {remote_path}", seed
97
- else:
98
- return upload_result, seed
99
-
100
- # App-Titel mit Modell- und Präzisionsinformationen
101
- APP_TITLE = f"### Stable Diffusion - {os.path.basename(MODEL_REPO)} ({TORCH_DTYPE} auf {device})"
102
-
103
- # Gradio-App
104
- with gr.Blocks() as demo:
105
- gr.Markdown(APP_TITLE)
106
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
107
- width = gr.Slider(256, MAX_IMAGE_SIZE, step=64, value=512, label="Width")
108
- height = gr.Slider(256, MAX_IMAGE_SIZE, step=64, value=512, label="Height")
109
- guidance_scale = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="Guidance Scale")
110
- num_inference_steps = gr.Slider(1, 50, step=1, value=25, label="Inference Steps")
111
- seed = gr.Number(value=42, label="Seed")
112
- randomize_seed = gr.Checkbox(value=False, label="Randomize Seed")
113
- generate_button = gr.Button("Generate Image")
114
- output = gr.Text(label="Output")
115
-
116
- # Klick-Event für die Generierung
117
- generate_button.click(
118
- infer,
119
- inputs=[prompt, width, height, guidance_scale, num_inference_steps, seed, randomize_seed],
120
- outputs=[output, seed]
121
- )
122
-
123
- demo.launch()