Spaces:
Sleeping
Sleeping
add req per minute rate limit
Browse files
billy.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
|
| 3 |
-
|
|
|
|
| 4 |
import openai
|
| 5 |
import os
|
| 6 |
import gradio as gr
|
| 7 |
import typer
|
|
|
|
|
|
|
| 8 |
|
| 9 |
"""
|
| 10 |
Example of a one-turn conversation with a hillbilly chatbot.
|
|
@@ -12,10 +15,60 @@ Example of a one-turn conversation with a hillbilly chatbot.
|
|
| 12 |
|
| 13 |
VERSION = "1.0.0"
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""Hillbily chatbot that tranlates phrases to funny old sayings."""
|
| 18 |
-
print(f"date: {datetime.now()}, region: {region}, input: {input}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
messages = [
|
| 21 |
{
|
|
@@ -27,14 +80,14 @@ def billy(region: str, input: str):
|
|
| 27 |
CONTEXT = "What is a hillbillly colloquialism for: "
|
| 28 |
if input:
|
| 29 |
messages.append({"role": "user", "content": CONTEXT + input})
|
| 30 |
-
print(f"date: {datetime.now()}, messages: {messages}")
|
| 31 |
chat = openai.ChatCompletion.create(
|
| 32 |
model="gpt-3.5-turbo", messages=messages
|
| 33 |
)
|
| 34 |
|
| 35 |
reply = chat.choices[0].message.content
|
| 36 |
messages.append({"role": "assistant", "content": reply})
|
| 37 |
-
print(f"date: {datetime.now()}, reply: {reply}")
|
| 38 |
return reply
|
| 39 |
|
| 40 |
|
|
@@ -60,6 +113,7 @@ def main():
|
|
| 60 |
"United States",
|
| 61 |
],
|
| 62 |
label="Colloquial Region",
|
|
|
|
| 63 |
),
|
| 64 |
gr.Textbox(lines=5, label="Enter in a phrase to translate:"),
|
| 65 |
]
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
|
| 3 |
+
import datetime
|
| 4 |
+
import hashlib
|
| 5 |
import openai
|
| 6 |
import os
|
| 7 |
import gradio as gr
|
| 8 |
import typer
|
| 9 |
+
from typing import List
|
| 10 |
+
from typing import TypedDict
|
| 11 |
|
| 12 |
"""
|
| 13 |
Example of a one-turn conversation with a hillbilly chatbot.
|
|
|
|
| 15 |
|
| 16 |
VERSION = "1.0.0"
|
| 17 |
|
| 18 |
+
# We have these as globals because the inputs to the handler won't accept ints.
|
| 19 |
+
REQ_LIMIT_HITS = 6
|
| 20 |
+
REQ_LIMIT_MINUTES = 1
|
| 21 |
+
CLIENTS = {}
|
| 22 |
|
| 23 |
+
|
| 24 |
+
def limitReached(
|
| 25 |
+
history: List[datetime.datetime],
|
| 26 |
+
limitHits: int,
|
| 27 |
+
limitMinutes: int,
|
| 28 |
+
ts: datetime.datetime,
|
| 29 |
+
) -> bool:
|
| 30 |
+
"""
|
| 31 |
+
Check whether the limit has been reached. This function mutates
|
| 32 |
+
the input history List
|
| 33 |
+
"""
|
| 34 |
+
timeLimit = ts - datetime.timedelta(minutes=limitMinutes)
|
| 35 |
+
for d in history:
|
| 36 |
+
if d < timeLimit:
|
| 37 |
+
history.remove(d)
|
| 38 |
+
|
| 39 |
+
# print(
|
| 40 |
+
# f"history_len: {len(history)}, limit_reached: {len(history) > limitHits}"
|
| 41 |
+
# )
|
| 42 |
+
return len(history) > limitHits
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def checkClient(clients: TypedDict, cookie: str) -> (bool, str):
|
| 46 |
+
now = datetime.datetime.now()
|
| 47 |
+
hsh = hashlib.sha256(bytes(cookie, "UTF-8")).hexdigest()
|
| 48 |
+
if hsh not in clients:
|
| 49 |
+
print(f"cookie: {cookie}")
|
| 50 |
+
clients[hsh] = [now]
|
| 51 |
+
else:
|
| 52 |
+
clients[hsh].append(now)
|
| 53 |
+
if limitReached(clients[hsh], REQ_LIMIT_HITS, REQ_LIMIT_MINUTES, now):
|
| 54 |
+
print(f"date: {now}, client: {hsh}, message: 'limit reached'")
|
| 55 |
+
return True, "ERROR: REQUESTS PER MINIUTE LIMIT REACHED"
|
| 56 |
+
|
| 57 |
+
return False, ""
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def billy(
|
| 61 |
+
region: str,
|
| 62 |
+
input: str,
|
| 63 |
+
request: gr.Request,
|
| 64 |
+
) -> str:
|
| 65 |
"""Hillbily chatbot that tranlates phrases to funny old sayings."""
|
| 66 |
+
print(f"date: {datetime.datetime.now()}, region: {region}, input: {input}")
|
| 67 |
+
|
| 68 |
+
cookie = request.headers["cookie"]
|
| 69 |
+
err, msg = checkClient(CLIENTS, cookie)
|
| 70 |
+
if err:
|
| 71 |
+
return msg
|
| 72 |
|
| 73 |
messages = [
|
| 74 |
{
|
|
|
|
| 80 |
CONTEXT = "What is a hillbillly colloquialism for: "
|
| 81 |
if input:
|
| 82 |
messages.append({"role": "user", "content": CONTEXT + input})
|
| 83 |
+
print(f"date: {datetime.datetime.now()}, messages: {messages}")
|
| 84 |
chat = openai.ChatCompletion.create(
|
| 85 |
model="gpt-3.5-turbo", messages=messages
|
| 86 |
)
|
| 87 |
|
| 88 |
reply = chat.choices[0].message.content
|
| 89 |
messages.append({"role": "assistant", "content": reply})
|
| 90 |
+
print(f"date: {datetime.datetime.now()}, reply: {reply}")
|
| 91 |
return reply
|
| 92 |
|
| 93 |
|
|
|
|
| 113 |
"United States",
|
| 114 |
],
|
| 115 |
label="Colloquial Region",
|
| 116 |
+
value="United States",
|
| 117 |
),
|
| 118 |
gr.Textbox(lines=5, label="Enter in a phrase to translate:"),
|
| 119 |
]
|