smdbs commited on
Commit
cd4e389
·
1 Parent(s): 613e0ef

feat: add prometheus client stuff

Browse files
Files changed (2) hide show
  1. app.py +61 -34
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,19 +1,36 @@
 
1
  from pathlib import Path
2
- from PIL import Image
 
3
  import gradio as gr
4
  import numpy as np
5
- import os
6
- from typing import Tuple, Union
7
  import torch
 
8
  from huggingface_hub import InferenceClient
9
- from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  MAX_SEED = np.iinfo(np.int32).max
12
  MAX_IMAGE_SIZE = 1280
13
 
14
-
15
  # Make sure to set the environment variable HF_TOKEN to your Hugging Face token for using InferenceClient
16
  HF_TOKEN = os.environ.get("HF_TOKEN", "YOUR_HF_TOKEN")
 
17
  model_name = "stabilityai/stable-diffusion-2-1-base"
18
  client = InferenceClient(model_name, token=HF_TOKEN)
19
 
@@ -50,34 +67,41 @@ def sd_2_1_base(
50
  seed = np.random.randint(0, MAX_SEED)
51
  generator = torch.Generator().manual_seed(seed)
52
 
53
- if is_local:
54
- if scheduler is None or pipe is None:
55
- load_model()
56
-
57
- image = pipe(
58
- prompt,
59
- negative_prompt=negative_prompt,
60
- guidance_scale=guidance_scale,
61
- num_inference_steps=num_inference_steps,
62
- width=width,
63
- height=height,
64
- generator=generator,
65
- ).images[0]
66
-
67
- return image, seed
68
-
69
- else:
70
- output = client.text_to_image(
71
- prompt,
72
- negative_prompt=negative_prompt,
73
- seed=seed,
74
- randomize_seed=randomize_seed,
75
- guidance_scale=guidance_scale,
76
- num_inference_steps=num_inference_steps,
77
- width=width,
78
- height=height,
79
- )
80
- return output, seed
 
 
 
 
 
 
 
81
 
82
 
83
  with gr.Blocks() as ui:
@@ -180,4 +204,7 @@ with gr.Blocks() as ui:
180
  )
181
 
182
  if __name__ == "__main__":
183
- ui.launch(share=True)
 
 
 
 
1
+ import os
2
  from pathlib import Path
3
+ from typing import Tuple, Union
4
+
5
  import gradio as gr
6
  import numpy as np
 
 
7
  import torch
8
+ from diffusers import EulerDiscreteScheduler, StableDiffusionPipeline
9
  from huggingface_hub import InferenceClient
10
+ from PIL import Image
11
+ from prometheus_client import Counter, Summary, start_http_server
12
+
13
+ # metrics
14
+ REQUEST_COUNTER = Counter("app_requests_total", "Total number of requests")
15
+ LOCAL_COUNTER = Counter("app_local_requests_total", "Total number of local requests")
16
+ API_COUNTER = Counter("app_api_requests_total", "Total number of API requests")
17
+ SUCCESSFUL_REQUESTS = Counter(
18
+ "app_successful_requests_total", "Total number of successful requests"
19
+ )
20
+ FAILED_REQUESTS = Counter(
21
+ "app_failed_requests_total", "Total number of failed requests"
22
+ )
23
+ REQUEST_DURATION = Summary(
24
+ "app_request_duration_seconds", "Time spent processing request"
25
+ )
26
+
27
 
28
  MAX_SEED = np.iinfo(np.int32).max
29
  MAX_IMAGE_SIZE = 1280
30
 
 
31
  # Make sure to set the environment variable HF_TOKEN to your Hugging Face token for using InferenceClient
32
  HF_TOKEN = os.environ.get("HF_TOKEN", "YOUR_HF_TOKEN")
33
+ print(f"Using Hugging Face token: {HF_TOKEN}")
34
  model_name = "stabilityai/stable-diffusion-2-1-base"
35
  client = InferenceClient(model_name, token=HF_TOKEN)
36
 
 
67
  seed = np.random.randint(0, MAX_SEED)
68
  generator = torch.Generator().manual_seed(seed)
69
 
70
+ REQUEST_COUNTER.inc()
71
+ with REQUEST_DURATION.time():
72
+ try:
73
+ if is_local:
74
+ LOCAL_COUNTER.inc()
75
+
76
+ image = pipe(
77
+ prompt,
78
+ negative_prompt=negative_prompt,
79
+ guidance_scale=guidance_scale,
80
+ num_inference_steps=num_inference_steps,
81
+ width=width,
82
+ height=height,
83
+ generator=generator,
84
+ ).images[0]
85
+
86
+ return image, seed
87
+
88
+ else:
89
+ API_COUNTER.inc()
90
+
91
+ output = client.text_to_image(
92
+ prompt,
93
+ negative_prompt=negative_prompt,
94
+ seed=seed,
95
+ randomize_seed=randomize_seed,
96
+ guidance_scale=guidance_scale,
97
+ num_inference_steps=num_inference_steps,
98
+ width=width,
99
+ height=height,
100
+ )
101
+ return output, seed
102
+ except Exception as e:
103
+ FAILED_REQUESTS.inc()
104
+ return str(e), seed
105
 
106
 
107
  with gr.Blocks() as ui:
 
204
  )
205
 
206
  if __name__ == "__main__":
207
+ if scheduler is None or pipe is None:
208
+ load_model()
209
+
210
+ ui.launch()
requirements.txt CHANGED
@@ -14,4 +14,6 @@ transformers==4.45.0
14
  accelerate==0.30.0
15
  diffusers==0.30.2
16
  scipy==1.13.1
17
- safetensors==0.4.5
 
 
 
14
  accelerate==0.30.0
15
  diffusers==0.30.2
16
  scipy==1.13.1
17
+ safetensors==0.4.5
18
+
19
+ prometheus_client