next-social commited on
Commit
ad223ef
1 Parent(s): 9dccf54
Files changed (2) hide show
  1. app.py +24 -3
  2. set.py +22 -0
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  from datasets import load_dataset
5
  from PIL import Image
6
 
 
7
  # from model import get_sd_small, get_sd_tiny, get_sd_every
8
  from trans_google import google_translator
9
  import replicate
@@ -52,7 +53,6 @@ re_sampler = [
52
  rand = random.Random()
53
  translator = google_translator()
54
 
55
-
56
  # tiny_pipe = get_sd_tiny()
57
  # small_pipe = get_sd_small()
58
  # every_pipe = get_sd_every()
@@ -66,8 +66,29 @@ translator = google_translator()
66
  # else:
67
  # return every_pipe
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- def infer(prompt: str, negative: str, width: int, height: int, sampler: str, steps: int, seed: int, scale):
71
  global is_gpu_busy
72
 
73
  if seed == 0:
@@ -350,7 +371,7 @@ with block:
350
  Also Change params to have a try <br />
351
  more size may cost more time. <br />
352
  It's just a simplified demo, you can use more advanced features optimize image quality <br />"""
353
- tutorial_link = "https://docs.cworld.ai/docs/cworld-ai/%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B/stable-diffusion/"
354
 
355
  gr.HTML(
356
  f"""
 
4
  from datasets import load_dataset
5
  from PIL import Image
6
 
7
+ from set import ExpiringMap
8
  # from model import get_sd_small, get_sd_tiny, get_sd_every
9
  from trans_google import google_translator
10
  import replicate
 
53
  rand = random.Random()
54
  translator = google_translator()
55
 
 
56
  # tiny_pipe = get_sd_tiny()
57
  # small_pipe = get_sd_small()
58
  # every_pipe = get_sd_every()
 
66
  # else:
67
  # return every_pipe
68
 
69
+ time_client_map = ExpiringMap()
70
+ count_client_map = ExpiringMap()
71
+
72
+
73
+ def infer(prompt: str, negative: str, width: int, height: int, sampler: str,
74
+ steps: int, seed: int, scale, request: gr.Request):
75
+ client_ip = request.client.host
76
+ if client_ip != '127.0.0.1' and client_ip != 'localhost' and client_ip != '0.0.0.0':
77
+ if time_client_map.get(client_ip):
78
+ return "Too many requests from this IP, please try again later."
79
+ else:
80
+ time_client_map.put(client_ip, 1, 10) # 添加一个过期时间为 10 秒的项
81
+
82
+ count = count_client_map.get(client_ip)
83
+ if count is None:
84
+ count = 0
85
+
86
+ count += 1
87
+ if count > 5:
88
+ return "Too many requests from this IP, please try again later."
89
+ else:
90
+ count_client_map.put(client_ip, count, 24 * 60 * 60) # 添加一个过期时间为 24 小时的项
91
 
 
92
  global is_gpu_busy
93
 
94
  if seed == 0:
 
371
  Also Change params to have a try <br />
372
  more size may cost more time. <br />
373
  It's just a simplified demo, you can use more advanced features optimize image quality <br />"""
374
+ tutorial_link = "https://docs.cworld.ai/docs/cworld-ai/quick-start-stable-diffusion"
375
 
376
  gr.HTML(
377
  f"""
set.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime, timedelta
2
+
3
+
4
+ class ExpiringMap:
5
+ def __init__(self):
6
+ self.data = defaultdict(dict)
7
+
8
+ def put(self, key, value, expire_time):
9
+ current_time = datetime.now()
10
+ expire_timestamp = current_time + timedelta(seconds=expire_time)
11
+ self.data[key] = {'value': value, 'expire_time': expire_timestamp}
12
+
13
+ def get(self, key):
14
+ current_time = datetime.now()
15
+ if key in self.data and self.data[key]['expire_time'] > current_time:
16
+ return self.data[key]['value']
17
+ else:
18
+ return None
19
+
20
+ def remove(self, key):
21
+ if key in self.data:
22
+ del self.data[key]