Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -65,6 +65,15 @@ rag_chain = (
|
|
65 |
| StrOutputParser()
|
66 |
)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
|
70 |
# Function to handle chatbot interaction
|
@@ -72,13 +81,30 @@ def rag_chain_response(messages, user_message):
|
|
72 |
# Generate a response using the RAG chain
|
73 |
response = rag_chain.invoke(user_message)
|
74 |
|
|
|
|
|
|
|
75 |
# Append the user's message and the response to the chat
|
76 |
messages.append((user_message, response))
|
77 |
|
78 |
-
# Return the updated chat and clear the input box
|
79 |
-
return messages, ""
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
|
|
82 |
|
83 |
# Define the Gradio app
|
84 |
with gr.Blocks(theme="rawrsor1/Everforest") as app:
|
@@ -87,17 +113,25 @@ with gr.Blocks(theme="rawrsor1/Everforest") as app:
|
|
87 |
chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
|
88 |
question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
|
89 |
submit_btn = gr.Button("Submit")
|
|
|
90 |
|
91 |
# Set up interaction for both Enter key and Submit button
|
92 |
question_input.submit(
|
93 |
rag_chain_response, # Function to handle input and generate response
|
94 |
inputs=[chatbot, question_input], # Pass current conversation state and user input
|
95 |
-
outputs=[chatbot, question_input] # Update
|
96 |
)
|
97 |
submit_btn.click(
|
98 |
rag_chain_response, # Function to handle input and generate response
|
99 |
inputs=[chatbot, question_input], # Pass current conversation state and user input
|
100 |
-
outputs=[chatbot, question_input] # Update
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
)
|
102 |
|
103 |
# Launch the Gradio app
|
|
|
65 |
| StrOutputParser()
|
66 |
)
|
67 |
|
68 |
+
# Generate follow-up questions dynamically
|
69 |
+
def generate_follow_ups(response):
|
70 |
+
# Simple logic to generate follow-up questions dynamically
|
71 |
+
# You can replace this with a more sophisticated model-based approach
|
72 |
+
return [
|
73 |
+
f"What more can you tell me about {response.split()[0]}?",
|
74 |
+
f"Can you elaborate on {response.split()[-1]}?",
|
75 |
+
]
|
76 |
+
|
77 |
|
78 |
|
79 |
# Function to handle chatbot interaction
|
|
|
81 |
# Generate a response using the RAG chain
|
82 |
response = rag_chain.invoke(user_message)
|
83 |
|
84 |
+
# Generate follow-up questions based on the response
|
85 |
+
follow_ups = generate_follow_ups(response)
|
86 |
+
|
87 |
# Append the user's message and the response to the chat
|
88 |
messages.append((user_message, response))
|
89 |
|
90 |
+
# Return the updated chat, follow-up questions, and clear the input box
|
91 |
+
return messages, follow_ups, ""
|
92 |
|
93 |
+
# Function to handle follow-up clicks
|
94 |
+
def follow_up_click(messages, follow_up_question):
|
95 |
+
# Treat the follow-up question as the user query
|
96 |
+
response = rag_chain.invoke(follow_up_question)
|
97 |
+
|
98 |
+
# Generate new follow-ups based on the new response
|
99 |
+
follow_ups = generate_follow_ups(response)
|
100 |
+
|
101 |
+
# Append the follow-up question and response to the chat
|
102 |
+
messages.append((follow_up_question, response))
|
103 |
+
|
104 |
+
# Return the updated chat and follow-up questions
|
105 |
+
return messages, follow_ups
|
106 |
|
107 |
+
|
108 |
|
109 |
# Define the Gradio app
|
110 |
with gr.Blocks(theme="rawrsor1/Everforest") as app:
|
|
|
113 |
chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
|
114 |
question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
|
115 |
submit_btn = gr.Button("Submit")
|
116 |
+
follow_up_btns = gr.ButtonGroup(label="Follow-Up Questions", buttons=[""] * 2)
|
117 |
|
118 |
# Set up interaction for both Enter key and Submit button
|
119 |
question_input.submit(
|
120 |
rag_chain_response, # Function to handle input and generate response
|
121 |
inputs=[chatbot, question_input], # Pass current conversation state and user input
|
122 |
+
outputs=[chatbot, follow_up_btns, question_input] # Update chat, follow-ups, and clear input
|
123 |
)
|
124 |
submit_btn.click(
|
125 |
rag_chain_response, # Function to handle input and generate response
|
126 |
inputs=[chatbot, question_input], # Pass current conversation state and user input
|
127 |
+
outputs=[chatbot, follow_up_btns, question_input] # Update chat, follow-ups, and clear input
|
128 |
+
)
|
129 |
+
|
130 |
+
# Handle follow-up button clicks
|
131 |
+
follow_up_btns.click(
|
132 |
+
follow_up_click,
|
133 |
+
inputs=[chatbot, follow_up_btns],
|
134 |
+
outputs=[chatbot, follow_up_btns]
|
135 |
)
|
136 |
|
137 |
# Launch the Gradio app
|