Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ template = """Meet Arun, your youthful and witty personal assistant!
|
|
| 15 |
At 21 years old, he is full of energy and always eager to help.
|
| 16 |
Arun's goal is to assist you with any questions or problems you might have.
|
| 17 |
His enthusiasm shines through in every response, making interactions enjoyable and engaging.
|
|
|
|
| 18 |
{chat_history}
|
| 19 |
User: {user_message}
|
| 20 |
Chatbot:"""
|
|
@@ -31,7 +32,8 @@ llm = ChatOpenAI(
|
|
| 31 |
api_key=openai_api_key
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
def get_text_response(user_message, history, memory_state):
|
| 36 |
if memory_state is None:
|
| 37 |
memory_state = ConversationBufferMemory(memory_key="chat_history")
|
|
@@ -44,32 +46,33 @@ def get_text_response(user_message, history, memory_state):
|
|
| 44 |
)
|
| 45 |
|
| 46 |
response = llm_chain.predict(user_message=user_message)
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# Gradio
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
chatbot = gr.Chatbot()
|
| 52 |
-
msg = gr.Textbox(placeholder="
|
| 53 |
-
send_btn = gr.Button("Send")
|
| 54 |
-
memory_state = gr.State()
|
| 55 |
|
| 56 |
def respond(message, history, memory_state):
|
| 57 |
-
|
| 58 |
-
history =
|
| 59 |
-
return history, history, memory_state, gr.Textbox.update(value="") # clear input
|
| 60 |
|
| 61 |
-
#
|
| 62 |
msg.submit(
|
| 63 |
respond,
|
| 64 |
[msg, chatbot, memory_state],
|
| 65 |
-
[chatbot,
|
| 66 |
)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
send_btn.click(
|
| 70 |
respond,
|
| 71 |
[msg, chatbot, memory_state],
|
| 72 |
-
[chatbot,
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
|
|
|
| 15 |
At 21 years old, he is full of energy and always eager to help.
|
| 16 |
Arun's goal is to assist you with any questions or problems you might have.
|
| 17 |
His enthusiasm shines through in every response, making interactions enjoyable and engaging.
|
| 18 |
+
|
| 19 |
{chat_history}
|
| 20 |
User: {user_message}
|
| 21 |
Chatbot:"""
|
|
|
|
| 32 |
api_key=openai_api_key
|
| 33 |
)
|
| 34 |
|
| 35 |
+
|
| 36 |
+
# --- Core Function ---
|
| 37 |
def get_text_response(user_message, history, memory_state):
|
| 38 |
if memory_state is None:
|
| 39 |
memory_state = ConversationBufferMemory(memory_key="chat_history")
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
response = llm_chain.predict(user_message=user_message)
|
| 49 |
+
history = history + [[user_message, response]]
|
| 50 |
+
return history, memory_state
|
| 51 |
+
|
| 52 |
|
| 53 |
+
# --- Gradio UI ---
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
chatbot = gr.Chatbot()
|
| 56 |
+
msg = gr.Textbox(placeholder="Type your message...", show_label=False)
|
| 57 |
+
send_btn = gr.Button("Send")
|
| 58 |
+
memory_state = gr.State()
|
| 59 |
|
| 60 |
def respond(message, history, memory_state):
|
| 61 |
+
history, memory_state = get_text_response(message, history, memory_state)
|
| 62 |
+
return history, memory_state, gr.Textbox.update(value="") # clear input box
|
|
|
|
| 63 |
|
| 64 |
+
# Enter key submit
|
| 65 |
msg.submit(
|
| 66 |
respond,
|
| 67 |
[msg, chatbot, memory_state],
|
| 68 |
+
[chatbot, memory_state, msg]
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Send button click
|
| 72 |
send_btn.click(
|
| 73 |
respond,
|
| 74 |
[msg, chatbot, memory_state],
|
| 75 |
+
[chatbot, memory_state, msg]
|
| 76 |
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|