refoundd commited on
Commit
f225a3f
·
verified ·
1 Parent(s): c4270b4

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +7 -61
handler.py CHANGED
@@ -9,53 +9,6 @@ import time
9
  import uuid
10
  from huggingface_hub import HfApi
11
 
12
- from pyngrok import ngrok
13
- import subprocess
14
- from fastapi import FastAPI
15
- from fastapi.responses import FileResponse
16
- import uvicorn
17
- import threading
18
-
19
- image_directory='./images'
20
- if not os.path.exists(image_directory):
21
- os.makedirs(image_directory)
22
-
23
- app = FastAPI()
24
-
25
- @app.get("/images/{image_name}")
26
- async def get_image(image_name: str):
27
- image_path = os.path.join(image_directory, image_name)
28
-
29
- if os.path.exists(image_path):
30
- return FileResponse(image_path)
31
- else:
32
- return {"error": "Image not found"}
33
-
34
- def run_uvicorn():
35
- uvicorn.run(app, host="127.0.0.1", port=6000)
36
-
37
- uvicorn_thread = threading.Thread(target=run_uvicorn)
38
- uvicorn_thread.daemon = True
39
- uvicorn_thread.start()
40
-
41
- authtoken = "2cvqFKWc1eb9b0aN7pRLDUBfEtC_2FUehxFL8CAKXRkW3Hfjo"
42
- commands = [
43
- # "snap install ngrok",
44
- f"ngrok config add-authtoken {authtoken}"
45
- ]
46
- for command in commands:
47
- try:
48
- subprocess.run(command, shell=True, check=True)
49
- logger.info(f"SUCCESS CMD: {command}")
50
- except subprocess.CalledProcessError as e:
51
- logger.info(f"Failed CMD: {e}")
52
- def run_ngrok():
53
- subprocess.run(["ngrok", "http", "6000"])
54
- ngrok_thread = threading.Thread(target=run_ngrok)
55
- ngrok_thread.daemon = True
56
- ngrok_thread.start()
57
- logger.info("ngrok is running in the background")
58
-
59
  class EndpointHandler:
60
  def __init__(self, path=""):
61
  self.pipe = FluxPipeline.from_pretrained(
@@ -69,13 +22,7 @@ class EndpointHandler:
69
  self.pipe.vae = torch.compile(
70
  self.pipe.vae, mode="max-autotune-no-cudagraphs",
71
  )
72
- # ngrok
73
- self.public_url = os.getcwd()
74
- # self.public_url = ngrok.connect(6000).public_url
75
- # subprocess.Popen(['ngrok', 'http', '5000'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76
-
77
- logger.info("Ngrok is running in the background.")
78
-
79
 
80
  def __call__(self, data: Dict[str, Any]) -> str:
81
  logger.info(f"Received incoming request with {data=}")
@@ -89,7 +36,8 @@ class EndpointHandler:
89
  "Provided input body must contain either the key `inputs` or `prompt` with the"
90
  " prompt to use for the image generation, and it needs to be a non-empty string."
91
  )
92
-
 
93
  parameters = data.pop("parameters", {})
94
 
95
  num_inference_steps = parameters.get("num_inference_steps", 28)
@@ -100,7 +48,9 @@ class EndpointHandler:
100
  # seed generator (seed cannot be provided as is but via a generator)
101
  seed = parameters.get("seed", 0)
102
  generator = torch.manual_seed(seed)
 
103
  start_time = time.time()
 
104
  result = self.pipe( # type: ignore
105
  prompt,
106
  height=height,
@@ -112,10 +62,6 @@ class EndpointHandler:
112
  end_time = time.time()
113
  time_taken = end_time - start_time
114
  print(f"Time taken: {time_taken:.2f} seconds")
115
- filename = f"{uuid.uuid4()}.png"
116
- image_path = f"/images/{filename}"
117
-
118
- result.save(image_path)
119
- image_url = f"{self.public_url+image_path}"
120
 
121
- return image_url
 
9
  import uuid
10
  from huggingface_hub import HfApi
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  class EndpointHandler:
13
  def __init__(self, path=""):
14
  self.pipe = FluxPipeline.from_pretrained(
 
22
  self.pipe.vae = torch.compile(
23
  self.pipe.vae, mode="max-autotune-no-cudagraphs",
24
  )
25
+ self.record=0
 
 
 
 
 
 
26
 
27
  def __call__(self, data: Dict[str, Any]) -> str:
28
  logger.info(f"Received incoming request with {data=}")
 
36
  "Provided input body must contain either the key `inputs` or `prompt` with the"
37
  " prompt to use for the image generation, and it needs to be a non-empty string."
38
  )
39
+ if prompt=="get_quene":
40
+ return self.record
41
  parameters = data.pop("parameters", {})
42
 
43
  num_inference_steps = parameters.get("num_inference_steps", 28)
 
48
  # seed generator (seed cannot be provided as is but via a generator)
49
  seed = parameters.get("seed", 0)
50
  generator = torch.manual_seed(seed)
51
+ self.record+=1
52
  start_time = time.time()
53
+ sleep(10)
54
  result = self.pipe( # type: ignore
55
  prompt,
56
  height=height,
 
62
  end_time = time.time()
63
  time_taken = end_time - start_time
64
  print(f"Time taken: {time_taken:.2f} seconds")
65
+ self.record-=1
 
 
 
 
66
 
67
+ return self.record