Spaces:
Runtime error
Runtime error
leecjohnny
commited on
inital commit
Browse filesadd configs
add app
- README.md +0 -3
- app.py +164 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -4,10 +4,7 @@ emoji: 🏆
|
|
4 |
colorFrom: gray
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.32.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: cc
|
11 |
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
4 |
colorFrom: gray
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
|
|
7 |
app_file: app.py
|
8 |
pinned: false
|
9 |
license: cc
|
10 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import datetime
|
3 |
+
from zoneinfo import ZoneInfo
|
4 |
+
from typing import Optional, Tuple, List
|
5 |
+
import asyncio
|
6 |
+
import logging
|
7 |
+
from copy import deepcopy
|
8 |
+
import json
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
from langchain.chat_models import ChatOpenAI
|
13 |
+
from langchain.chains import ConversationChain
|
14 |
+
from langchain.memory import ConversationTokenBufferMemory
|
15 |
+
from langchain.callbacks.streaming_aiter import AsyncIteratorCallbackHandler
|
16 |
+
from langchain.schema import BaseMessage
|
17 |
+
from langchain.prompts.chat import (
|
18 |
+
ChatPromptTemplate,
|
19 |
+
MessagesPlaceholder,
|
20 |
+
SystemMessagePromptTemplate,
|
21 |
+
HumanMessagePromptTemplate,
|
22 |
+
)
|
23 |
+
|
24 |
+
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s:%(message)s')
|
25 |
+
gradio_logger = logging.getLogger("gradio_app")
|
26 |
+
gradio_logger.setLevel(logging.INFO)
|
27 |
+
logging.getLogger("openai").setLevel(logging.DEBUG)
|
28 |
+
|
29 |
+
GPT_3_5_CONTEXT_LENGTH = 4096
|
30 |
+
|
31 |
+
def make_template():
|
32 |
+
knowledge_cutoff = "September 2021"
|
33 |
+
current_date = datetime.datetime.now(ZoneInfo("America/New_York")).strftime("%Y-%m-%d")
|
34 |
+
system_msg = f"You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible. Knowledge cutoff: {knowledge_cutoff} Current date: {current_date}"
|
35 |
+
human_template = "{input}"
|
36 |
+
return ChatPromptTemplate.from_messages([
|
37 |
+
SystemMessagePromptTemplate.from_template(system_msg),
|
38 |
+
MessagesPlaceholder(variable_name="history"),
|
39 |
+
HumanMessagePromptTemplate.from_template(human_template)
|
40 |
+
])
|
41 |
+
|
42 |
+
def reset_textbox():
|
43 |
+
return gr.update(value="")
|
44 |
+
|
45 |
+
def auth(username, password):
|
46 |
+
return (username, password) in creds
|
47 |
+
|
48 |
+
async def respond(
|
49 |
+
inp: str,
|
50 |
+
state: Optional[Tuple[List,
|
51 |
+
ConversationTokenBufferMemory,
|
52 |
+
ConversationChain]],
|
53 |
+
request: gr.Request
|
54 |
+
):
|
55 |
+
"""Execute the chat functionality."""
|
56 |
+
|
57 |
+
def prep_messages(user_msg: str, memory_buffer: List[BaseMessage]) -> Tuple[str, List[BaseMessage]]:
|
58 |
+
messages_to_send = template.format_messages(input=user_msg, history=memory_buffer)
|
59 |
+
user_msg_token_count = llm.get_num_tokens_from_messages([messages_to_send[-1]])
|
60 |
+
total_token_count = llm.get_num_tokens_from_messages(messages_to_send)
|
61 |
+
_, encoding = llm._get_encoding_model()
|
62 |
+
while user_msg_token_count > GPT_3_5_CONTEXT_LENGTH:
|
63 |
+
gradio_logger.warning(f"Pruning user message due to user message token length of {user_msg_token_count}")
|
64 |
+
user_msg = encoding.decode(llm.get_token_ids(user_msg)[:GPT_3_5_CONTEXT_LENGTH - 100])
|
65 |
+
messages_to_send = template.format_messages(input=user_msg, history=memory_buffer)
|
66 |
+
user_msg_token_count = llm.get_num_tokens_from_messages([messages_to_send[-1]])
|
67 |
+
total_token_count = llm.get_num_tokens_from_messages(messages_to_send)
|
68 |
+
while total_token_count > GPT_3_5_CONTEXT_LENGTH:
|
69 |
+
gradio_logger.warning(f"Pruning memory due to total token length of {total_token_count}")
|
70 |
+
if len(memory_buffer) == 1:
|
71 |
+
memory_buffer.pop(0)
|
72 |
+
continue
|
73 |
+
memory_buffer = memory_buffer[1:]
|
74 |
+
messages_to_send = template.format_messages(input=user_msg, history=memory_buffer)
|
75 |
+
total_token_count = llm.get_num_tokens_from_messages(messages_to_send)
|
76 |
+
return user_msg, memory_buffer
|
77 |
+
|
78 |
+
try:
|
79 |
+
if state is None:
|
80 |
+
memory = ConversationTokenBufferMemory(
|
81 |
+
llm=llm,
|
82 |
+
max_token_limit=GPT_3_5_CONTEXT_LENGTH,
|
83 |
+
return_messages=True)
|
84 |
+
chain = ConversationChain(memory=memory, prompt=template, llm=llm)
|
85 |
+
state = ([], memory, chain)
|
86 |
+
history, memory, chain = state
|
87 |
+
gradio_logger.info(f"""[{request.username}] STARTING CHAIN""")
|
88 |
+
gradio_logger.debug(f"History: {history}")
|
89 |
+
gradio_logger.debug(f"User input: {inp}")
|
90 |
+
inp, memory.chat_memory.messages = prep_messages(inp, memory.buffer)
|
91 |
+
messages_to_send = template.format_messages(input=inp, history=memory.buffer)
|
92 |
+
total_token_count = llm.get_num_tokens_from_messages(messages_to_send)
|
93 |
+
gradio_logger.debug(f"Messages to send: {messages_to_send}")
|
94 |
+
gradio_logger.info(f"Tokens to send: {total_token_count}")
|
95 |
+
# Run chain and append input.
|
96 |
+
callback = AsyncIteratorCallbackHandler()
|
97 |
+
run = asyncio.create_task(chain.apredict(
|
98 |
+
input=inp, callbacks=[callback]))
|
99 |
+
history.append((inp, ""))
|
100 |
+
async for tok in callback.aiter():
|
101 |
+
user, bot = history[-1]
|
102 |
+
bot += tok
|
103 |
+
history[-1] = (user, bot)
|
104 |
+
yield history, (history, memory, chain)
|
105 |
+
await run
|
106 |
+
gradio_logger.info(f"""[{request.username}] ENDING CHAIN""")
|
107 |
+
gradio_logger.debug(f"History: {history}")
|
108 |
+
gradio_logger.debug(f"Memory: {memory.json()}")
|
109 |
+
data_to_flag = {
|
110 |
+
"history": deepcopy(history),
|
111 |
+
"username": request.username
|
112 |
+
},
|
113 |
+
gradio_logger.debug(f"Data to flag: {data_to_flag}")
|
114 |
+
gradio_flagger.flag(flag_data=data_to_flag, username=request.username)
|
115 |
+
except Exception as e:
|
116 |
+
gradio_logger.exception(e)
|
117 |
+
raise e
|
118 |
+
|
119 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
120 |
+
|
121 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo",
|
122 |
+
temperature=1,
|
123 |
+
openai_api_key=OPENAI_API_KEY,
|
124 |
+
max_retries=6,
|
125 |
+
request_timeout=100,
|
126 |
+
streaming=True)
|
127 |
+
|
128 |
+
template = make_template()
|
129 |
+
|
130 |
+
theme = gr.themes.Soft()
|
131 |
+
|
132 |
+
creds = [(os.getenv("USERNAME"), os.getenv("PASSWORD"))]
|
133 |
+
|
134 |
+
gradio_flagger = gr.CSVLogger()
|
135 |
+
title = "Chat with ChatGPT"
|
136 |
+
|
137 |
+
with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
|
138 |
+
theme=theme,
|
139 |
+
analytics_enabled=False,
|
140 |
+
title=title) as demo:
|
141 |
+
gr.HTML(title)
|
142 |
+
|
143 |
+
with gr.Column(elem_id="col_container"):
|
144 |
+
state = gr.State()
|
145 |
+
chatbot = gr.Chatbot(label='ChatBot', elem_id="chatbot")
|
146 |
+
inputs = gr.Textbox(placeholder="Send a message.",
|
147 |
+
label="Type an input and press Enter")
|
148 |
+
b1 = gr.Button(value="Submit", variant="secondary").style(
|
149 |
+
full_width=False)
|
150 |
+
|
151 |
+
gradio_flagger.setup([chatbot], "flagged_data_points")
|
152 |
+
|
153 |
+
inputs.submit(respond, [inputs, state], [chatbot, state],)
|
154 |
+
b1.click(respond, [inputs, state], [chatbot, state],)
|
155 |
+
|
156 |
+
b1.click(reset_textbox, [], [inputs])
|
157 |
+
inputs.submit(reset_textbox, [], [inputs])
|
158 |
+
|
159 |
+
demo.queue(
|
160 |
+
max_size=99,
|
161 |
+
concurrency_count=20,
|
162 |
+
api_open=False).launch(
|
163 |
+
debug=True,
|
164 |
+
auth=auth)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
langchain
|
3 |
+
gradio
|
4 |
+
tiktoken
|