lunarflu HF Staff commited on
Commit
ee5699a
·
1 Parent(s): 3d72860

df 0.3 async requests test

Browse files
Files changed (1) hide show
  1. app.py +34 -5
app.py CHANGED
@@ -14,9 +14,13 @@ import matplotlib.pyplot as plt
14
  import matplotlib.image as mpimg
15
  import time
16
 
 
 
 
17
 
18
 
19
 
 
20
  #todos
21
  #alert
22
 
@@ -25,10 +29,31 @@ jojogan = gradio_client.Client("akhaliq/JoJoGAN")
25
 
26
  #token update
27
  DFIF_TOKEN = os.getenv('DFIF_TOKEN')
 
28
  #deepfloydif
29
  #df = Client("DeepFloyd/IF") #not reliable at the moment
30
- df = Client("huggingface-projects/IF", hf_token=DFIF_TOKEN)
31
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # Set up discord bot
34
  class MyClient(discord.Client):
@@ -89,7 +114,8 @@ class MyClient(discord.Client):
89
  # predict(img, model, api_name="/predict") -> output
90
  # im = jojogan.predict(img, model) -> output
91
  # im = jojogan.predict(attachment.url, style) -> output
92
-
 
93
  im = jojogan.predict(attachment.url, style)
94
  await message.reply(f'Here is the {style} version of it', file=discord.File(im))
95
  else:
@@ -124,10 +150,13 @@ class MyClient(discord.Client):
124
  current_time = int(time.time())
125
  random.seed(current_time)
126
  seed = random.randint(1, 10000)
127
-
128
- stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("llama", "blur", seed,1,7.0, 'smart100',50, api_name="/generate64")
129
 
130
  #stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("gradio written on a wall", "blur", 1,1,7.0, 'smart100',50, api_name="/generate64")
 
 
 
 
 
131
 
132
  # Assuming stage_1_results contains the path to the directory
133
  png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
 
14
  import matplotlib.image as mpimg
15
  import time
16
 
17
+ #----------------------------------------------------------------------------------------------------
18
+ #async requests (otherwise each new !deepfloydif will add another ~20s until the first output)
19
+ import aiohttp
20
 
21
 
22
 
23
+
24
  #todos
25
  #alert
26
 
 
29
 
30
  #token update
31
  DFIF_TOKEN = os.getenv('DFIF_TOKEN')
32
+ hf_token = DFIF_TOKEN # could clean this up
33
  #deepfloydif
34
  #df = Client("DeepFloyd/IF") #not reliable at the moment
35
+ #df = Client("huggingface-projects/IF", hf_token=DFIF_TOKEN) # testing replace with new async client
36
+ df = AsyncGradioClient("huggingface-projects/IF", hf_token=DFIF_TOKEN)
37
+
38
+ #----------------------------------------------------------------------------------------------------
39
+ class AsyncGradioClient(gradio_client.Client):
40
+ async def predict_async(self, *args, api_name=None, fn_index=None):
41
+ if not self.is_live:
42
+ raise Exception("Cannot call predict() on a non-live interface. "
43
+ "Please call launch() to launch the interface first.")
44
+ if api_name is None:
45
+ api_name = self.default_api_name
46
+ if fn_index is None:
47
+ fn_index = self.default_fn_index
48
+ url = self.api_url + api_name
49
+ headers = {"Authorization": "Bearer " + self.hf_token} if self.hf_token else {}
50
+ async with aiohttp.ClientSession() as session:
51
+ async with session.post(url, json={"args": args, "fn_index": fn_index}, headers=headers) as response:
52
+ response_json = await response.json()
53
+ if "error" in response_json:
54
+ raise Exception(response_json["error"])
55
+ return response_json["output"]
56
+ #----------------------------------------------------------------------------------------------------
57
 
58
  # Set up discord bot
59
  class MyClient(discord.Client):
 
114
  # predict(img, model, api_name="/predict") -> output
115
  # im = jojogan.predict(img, model) -> output
116
  # im = jojogan.predict(attachment.url, style) -> output
117
+
118
+ #im = await jojogan.predict_async(attachment.url, style) # new async, could test
119
  im = jojogan.predict(attachment.url, style)
120
  await message.reply(f'Here is the {style} version of it', file=discord.File(im))
121
  else:
 
150
  current_time = int(time.time())
151
  random.seed(current_time)
152
  seed = random.randint(1, 10000)
 
 
153
 
154
  #stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("gradio written on a wall", "blur", 1,1,7.0, 'smart100',50, api_name="/generate64")
155
+ stage_1_results, stage_1_param_path, stage_1_result_path = df.predict("llama", "blur", seed,1,7.0, 'smart100',50, api_name="/generate64")
156
+
157
+ stage_1_results, stage_1_param_path, stage_1_result_path = df.predict_async("llama", "blur", seed,1,7.0, 'smart100',50, api_name="/generate64")
158
+
159
+
160
 
161
  # Assuming stage_1_results contains the path to the directory
162
  png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]