Spaces:
Runtime error
Runtime error
leecjohnny
commited on
update to HF dataset and secrets for login
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import asyncio
|
|
| 6 |
import logging
|
| 7 |
from copy import deepcopy
|
| 8 |
import json
|
|
|
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
|
|
@@ -49,7 +50,8 @@ 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."""
|
|
@@ -82,8 +84,9 @@ async def respond(
|
|
| 82 |
max_token_limit=GPT_3_5_CONTEXT_LENGTH,
|
| 83 |
return_messages=True)
|
| 84 |
chain = ConversationChain(memory=memory, prompt=template, llm=llm)
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
gradio_logger.info(f"""[{request.username}] STARTING CHAIN""")
|
| 88 |
gradio_logger.debug(f"History: {history}")
|
| 89 |
gradio_logger.debug(f"User input: {inp}")
|
|
@@ -101,14 +104,16 @@ async def respond(
|
|
| 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)
|
|
@@ -117,6 +122,7 @@ async def respond(
|
|
| 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,
|
|
@@ -129,9 +135,9 @@ template = make_template()
|
|
| 129 |
|
| 130 |
theme = gr.themes.Soft()
|
| 131 |
|
| 132 |
-
creds = [(os.getenv("
|
| 133 |
|
| 134 |
-
gradio_flagger = gr.
|
| 135 |
title = "Chat with ChatGPT"
|
| 136 |
|
| 137 |
with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
|
|
@@ -139,7 +145,6 @@ with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;} #
|
|
| 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")
|
|
@@ -148,7 +153,7 @@ with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;} #
|
|
| 148 |
b1 = gr.Button(value="Submit", variant="secondary").style(
|
| 149 |
full_width=False)
|
| 150 |
|
| 151 |
-
gradio_flagger.setup([chatbot], "
|
| 152 |
|
| 153 |
inputs.submit(respond, [inputs, state], [chatbot, state],)
|
| 154 |
b1.click(respond, [inputs, state], [chatbot, state],)
|
|
|
|
| 6 |
import logging
|
| 7 |
from copy import deepcopy
|
| 8 |
import json
|
| 9 |
+
import uuid
|
| 10 |
|
| 11 |
import gradio as gr
|
| 12 |
|
|
|
|
| 50 |
inp: str,
|
| 51 |
state: Optional[Tuple[List,
|
| 52 |
ConversationTokenBufferMemory,
|
| 53 |
+
ConversationChain,
|
| 54 |
+
str]],
|
| 55 |
request: gr.Request
|
| 56 |
):
|
| 57 |
"""Execute the chat functionality."""
|
|
|
|
| 84 |
max_token_limit=GPT_3_5_CONTEXT_LENGTH,
|
| 85 |
return_messages=True)
|
| 86 |
chain = ConversationChain(memory=memory, prompt=template, llm=llm)
|
| 87 |
+
session_id = str(uuid.uuid4())
|
| 88 |
+
state = ([], memory, chain, session_id)
|
| 89 |
+
history, memory, chain, session_id = state
|
| 90 |
gradio_logger.info(f"""[{request.username}] STARTING CHAIN""")
|
| 91 |
gradio_logger.debug(f"History: {history}")
|
| 92 |
gradio_logger.debug(f"User input: {inp}")
|
|
|
|
| 104 |
user, bot = history[-1]
|
| 105 |
bot += tok
|
| 106 |
history[-1] = (user, bot)
|
| 107 |
+
yield history, (history, memory, chain, session_id)
|
| 108 |
await run
|
| 109 |
gradio_logger.info(f"""[{request.username}] ENDING CHAIN""")
|
| 110 |
gradio_logger.debug(f"History: {history}")
|
| 111 |
gradio_logger.debug(f"Memory: {memory.json()}")
|
| 112 |
data_to_flag = {
|
| 113 |
"history": deepcopy(history),
|
| 114 |
+
"username": request.username,
|
| 115 |
+
"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
| 116 |
+
"session_id": session_id
|
| 117 |
},
|
| 118 |
gradio_logger.debug(f"Data to flag: {data_to_flag}")
|
| 119 |
gradio_flagger.flag(flag_data=data_to_flag, username=request.username)
|
|
|
|
| 122 |
raise e
|
| 123 |
|
| 124 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 125 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 126 |
|
| 127 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo",
|
| 128 |
temperature=1,
|
|
|
|
| 135 |
|
| 136 |
theme = gr.themes.Soft()
|
| 137 |
|
| 138 |
+
creds = [(os.getenv("CHAT_USERNAME"), os.getenv("CHAT_PASSWORD"))]
|
| 139 |
|
| 140 |
+
gradio_flagger = gr.HuggingFaceDatasetSaver(HF_TOKEN, "chats")
|
| 141 |
title = "Chat with ChatGPT"
|
| 142 |
|
| 143 |
with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
|
|
|
|
| 145 |
analytics_enabled=False,
|
| 146 |
title=title) as demo:
|
| 147 |
gr.HTML(title)
|
|
|
|
| 148 |
with gr.Column(elem_id="col_container"):
|
| 149 |
state = gr.State()
|
| 150 |
chatbot = gr.Chatbot(label='ChatBot', elem_id="chatbot")
|
|
|
|
| 153 |
b1 = gr.Button(value="Submit", variant="secondary").style(
|
| 154 |
full_width=False)
|
| 155 |
|
| 156 |
+
gradio_flagger.setup([chatbot], "chats")
|
| 157 |
|
| 158 |
inputs.submit(respond, [inputs, state], [chatbot, state],)
|
| 159 |
b1.click(respond, [inputs, state], [chatbot, state],)
|