Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,94 +1,54 @@
|
|
1 |
-
# main.py
|
2 |
-
|
3 |
import gradio as gr
|
4 |
from dotenv import load_dotenv
|
|
|
5 |
|
6 |
from database.db_handler import init_db
|
7 |
from langchain_logic.agent_setup import create_agent_executor
|
8 |
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
# ---
|
12 |
-
|
|
|
13 |
init_db()
|
14 |
-
|
15 |
-
agent_executor = create_agent_executor()
|
16 |
-
|
17 |
-
# --- Initial Bot Message ---
|
18 |
-
INITIAL_MESSAGE = """
|
19 |
-
Hey! I'm your appointment scheduling assistant. I can help you schedule, find, update, or delete appointments.
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
- **appointment_datetime**: The desired date and time in `YYYY-MM-DD HH:MM:SS` format.
|
25 |
-
- **service_type**: The reason for the visit (e.g., "Dental Checkup", "Haircut").
|
26 |
-
|
27 |
-
How can I help you today?
|
28 |
-
"""
|
29 |
|
|
|
30 |
|
31 |
-
#
|
32 |
-
def
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
# We convert it to the list-of-tuples format LangChain's agent expects.
|
39 |
-
chat_history_tuples = []
|
40 |
-
for user_msg, ai_msg in history:
|
41 |
-
# We only want to feed the actual conversational turns to the agent,
|
42 |
-
# not the initial greeting.
|
43 |
-
if ai_msg != INITIAL_MESSAGE:
|
44 |
-
chat_history_tuples.append(("human", user_msg))
|
45 |
-
chat_history_tuples.append(("ai", ai_msg))
|
46 |
|
|
|
47 |
response = agent_executor.invoke({
|
48 |
"input": message,
|
49 |
-
"chat_history":
|
50 |
})
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# The ChatInterface expects a single string response from the function.
|
53 |
-
# It will automatically append the user's message and this response to the chatbot's history.
|
54 |
-
return response["output"]
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
Function to provide the initial greeting.
|
60 |
-
It must return the data in the format the chatbot expects: a list of [user, bot] pairs.
|
61 |
-
"""
|
62 |
-
# FIX: Return the history in the correct format.
|
63 |
-
return [[None, INITIAL_MESSAGE]]
|
64 |
|
65 |
-
|
66 |
-
# --- Launch the Gradio App ---
|
67 |
if __name__ == "__main__":
|
68 |
-
|
69 |
-
|
70 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="Appointment Bot") as demo:
|
71 |
-
# Create a Chatbot component that will be populated on load.
|
72 |
-
chatbot = gr.Chatbot(label="Appointment Bot", elem_id="chatbot", height=600)
|
73 |
-
|
74 |
-
# Create the full ChatInterface.
|
75 |
-
chat_interface = gr.ChatInterface(
|
76 |
-
fn=chat_interface_fn,
|
77 |
-
chatbot=chatbot,
|
78 |
-
examples=[
|
79 |
-
["Find all appointments for 'John Doe'"],
|
80 |
-
["I'd like to book a 'Financial Consultation' for 2024-12-25 at 14:00:00. My name is Jane Doe and my number is 555-876-5432."],
|
81 |
-
["Please delete all 'Maintenance Check' appointments."],
|
82 |
-
["Update the phone number for Jane Doe to 555-999-0000"]
|
83 |
-
],
|
84 |
-
title="Conversational Appointment Scheduler"
|
85 |
-
)
|
86 |
-
|
87 |
-
# Use a 'load' event to populate the initial greeting using the greet function.
|
88 |
-
demo.load(
|
89 |
-
fn=greet,
|
90 |
-
inputs=None,
|
91 |
-
outputs=chatbot
|
92 |
-
)
|
93 |
-
|
94 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from dotenv import load_dotenv
|
3 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
4 |
|
5 |
from database.db_handler import init_db
|
6 |
from langchain_logic.agent_setup import create_agent_executor
|
7 |
|
8 |
+
# Load environment variables from .env for local development
|
9 |
+
# On Hugging Face, this line will do nothing, which is what we want.
|
10 |
+
load_dotenv()
|
11 |
|
12 |
+
# --- App Setup ---
|
13 |
+
# Initialize the database and table if they don't exist
|
14 |
+
print("Initializing database...")
|
15 |
init_db()
|
16 |
+
print("Database initialized.")
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Create the agent executor
|
19 |
+
agent_executor = create_agent_executor()
|
20 |
+
print("Agent Executor created.")
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# --- Gradio Interface ---
|
23 |
|
24 |
+
# We need to manage chat history
|
25 |
+
def respond(message, chat_history):
|
26 |
+
# Convert Gradio's chat history to LangChain's format
|
27 |
+
history_langchain_format = []
|
28 |
+
for human, ai in chat_history:
|
29 |
+
history_langchain_format.append(HumanMessage(content=human))
|
30 |
+
history_langchain_format.append(AIMessage(content=ai))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Invoke the agent
|
33 |
response = agent_executor.invoke({
|
34 |
"input": message,
|
35 |
+
"chat_history": history_langchain_format
|
36 |
})
|
37 |
+
|
38 |
+
# Append the new interaction to the chat history
|
39 |
+
chat_history.append((message, response['output']))
|
40 |
+
return "", chat_history
|
41 |
|
|
|
|
|
|
|
42 |
|
43 |
+
# Build the Gradio UI
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
gr.Markdown("# Appointment Scheduling Assistant")
|
46 |
+
chatbot = gr.Chatbot()
|
47 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type your request here (e.g., 'show all appointments', 'book a haircut for Jane Doe')")
|
48 |
+
clear = gr.Button("Clear")
|
49 |
|
50 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
51 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
|
|
53 |
if __name__ == "__main__":
|
54 |
+
demo.launch(debug=True) # debug=True is for local testing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|