Spaces:
Sleeping
Sleeping
File size: 3,997 Bytes
e7bc31f f12f0aa 2e48201 e7bc31f 2e48201 e7bc31f f12f0aa e7bc31f f12f0aa 5899e24 f12f0aa 5899e24 e7bc31f f12f0aa 5899e24 f12f0aa 5899e24 f12f0aa 5899e24 f12f0aa 5899e24 f12f0aa 5899e24 f12f0aa 5899e24 f12f0aa e7bc31f f12f0aa 5899e24 f12f0aa 2b10752 e7bc31f 2b10752 e7bc31f f12f0aa e7bc31f f12f0aa e7bc31f 9ec7073 2b10752 9ec7073 2b10752 f12f0aa 2b10752 e7bc31f b0d6a45 e7bc31f 2e48201 e7bc31f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #!/usr/bin/env python
import datetime
import hashlib
import openai
import os
import gradio as gr
import typer
from typing import List
from typing import TypedDict
"""
Example of a one-turn conversation with a hillbilly chatbot.
"""
VERSION = "1.0.0"
# We have these as globals because the inputs to the handler won't accept ints.
ALL_REQ_LIMIT_HITS = 24
CLIENT_REQ_LIMIT_HITS = 6
REQ_LIMIT_MINUTES = 1
CLIENTS = {}
ALL_CLIENTS_TS = []
def limitReached(
history: List[datetime.datetime],
limitHits: int,
limitMinutes: int,
ts: datetime.datetime,
) -> bool:
"""
Check whether the limit has been reached. This function mutates
the input history List
"""
timeLimit = ts - datetime.timedelta(minutes=limitMinutes)
for d in history:
if d < timeLimit:
history.remove(d)
# print(
# f"history_len: {len(history)}, limit_reached: {len(history) > limitHits}"
# )
return len(history) > limitHits
def checkClient(clients: TypedDict, requester: str) -> (bool, str):
now = datetime.datetime.now()
hsh = hashlib.sha256(bytes(requester, "UTF-8")).hexdigest()
if hsh not in clients:
print(f"requester: {requester}")
clients[hsh] = [now]
else:
clients[hsh].append(now)
if limitReached(
clients[hsh], CLIENT_REQ_LIMIT_HITS, REQ_LIMIT_MINUTES, now
):
print(f"date: {now}, client: {hsh}, message: 'limit reached'")
return True, "ERROR: REQUESTS PER MINIUTE LIMIT REACHED"
# check the global clients request rate
ALL_CLIENTS_TS.append(now)
if limitReached(ALL_CLIENTS_TS, ALL_REQ_LIMIT_HITS, REQ_LIMIT_MINUTES, now):
return True, "ERROR: ALL API REQUESTS PER MINIUTE LIMIT REACHED"
return False, ""
def getRequester(request: gr.Request) -> str:
requester = request.client.host
if "user-agent" in request.headers:
requester = "".join([requester, "::", request.headers["user-agent"]])
return requester
def billy(
region: str,
input: str,
request: gr.Request,
) -> str:
"""Hillbily chatbot that tranlates phrases to funny old sayings."""
print(f"date: {datetime.datetime.now()}, region: {region}, input: {input}")
requester = getRequester(request)
err, msg = checkClient(CLIENTS, requester)
if err:
return msg
messages = [
{
"role": "system",
"content": f"You are a quirky hillbilly from rural {region} that speaks in colloquialisms.",
},
]
CONTEXT = "What is a hillbillly colloquialism for: "
if input:
messages.append({"role": "user", "content": CONTEXT + input})
print(f"date: {datetime.datetime.now()}, messages: {messages}")
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
print(f"date: {datetime.datetime.now()}, reply: {reply}")
return reply
def main():
apikey = os.getenv("OPENAI_API_KEY")
openai.api_key = apikey
# "Appalachia",
# "Breadbasket",
# "Ecotopia",
# "Dixie",
# "Foundry",
# "Mexamerica",
# "New England",
inputs = [
gr.Dropdown(
[
"Australia",
"Canada",
"England",
"Ireland",
"United States",
],
label="Colloquial Region",
value="United States",
),
gr.Textbox(lines=5, label="Enter in a phrase to translate:"),
]
outputs = gr.Textbox(label="I reckon I'd say:")
gr.Interface(
fn=billy,
inputs=inputs,
outputs=outputs,
title=f"Colloquial Chatterbot {VERSION}",
description="Things a hillbilly would say",
theme=gr.themes.Monochrome(),
).launch(share=False)
if __name__ == "__main__":
typer.run(main)
|