Spaces:
Sleeping
Sleeping
Update chatbot_ui.py
Browse files- chatbot_ui.py +17 -19
chatbot_ui.py
CHANGED
|
@@ -2,14 +2,16 @@ import gradio as gr # For building the chatbot UI
|
|
| 2 |
import random # For generating random responses
|
| 3 |
|
| 4 |
# Chatbot response function
|
| 5 |
-
def chatbot_response(user_input):
|
| 6 |
user_input = user_input.lower()
|
| 7 |
|
|
|
|
| 8 |
if user_input.strip() == "":
|
| 9 |
-
return "Welcome! How can I assist you with your studies today?"
|
| 10 |
|
|
|
|
| 11 |
if "time management" in user_input:
|
| 12 |
-
return "Time management is key! Try creating a prioritized to-do list and setting specific study blocks."
|
| 13 |
|
| 14 |
elif "study tips" in user_input:
|
| 15 |
tips = [
|
|
@@ -17,33 +19,29 @@ def chatbot_response(user_input):
|
|
| 17 |
"Use active recall and spaced repetition for better retention.",
|
| 18 |
"Set a specific goal for each study session."
|
| 19 |
]
|
| 20 |
-
return random.choice(tips)
|
| 21 |
|
| 22 |
elif "hello" in user_input or "hi" in user_input:
|
| 23 |
-
return "Hello! How can I help you with your studies today?"
|
| 24 |
|
| 25 |
else:
|
| 26 |
-
return "I'm here to assist with general academic questions. Feel free to ask about study tips, time management, or anything else!"
|
| 27 |
|
| 28 |
-
# Gradio interface
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
gr.Markdown("# Study Assistance Chatbot")
|
| 31 |
gr.Markdown("Welcome! Ask me anything related to your academic studies.")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
submit_button = gr.Button("Submit")
|
| 37 |
-
|
| 38 |
-
with gr.Column():
|
| 39 |
-
chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
# Launch the Gradio app
|
| 47 |
demo.launch()
|
| 48 |
|
| 49 |
-
|
|
|
|
| 2 |
import random # For generating random responses
|
| 3 |
|
| 4 |
# Chatbot response function
|
| 5 |
+
def chatbot_response(user_input, history):
|
| 6 |
user_input = user_input.lower()
|
| 7 |
|
| 8 |
+
# Handle empty input and greet the user
|
| 9 |
if user_input.strip() == "":
|
| 10 |
+
return "Welcome! How can I assist you with your studies today?", history
|
| 11 |
|
| 12 |
+
# Predefined responses based on user input
|
| 13 |
if "time management" in user_input:
|
| 14 |
+
return "Time management is key! Try creating a prioritized to-do list and setting specific study blocks.", history
|
| 15 |
|
| 16 |
elif "study tips" in user_input:
|
| 17 |
tips = [
|
|
|
|
| 19 |
"Use active recall and spaced repetition for better retention.",
|
| 20 |
"Set a specific goal for each study session."
|
| 21 |
]
|
| 22 |
+
return random.choice(tips), history
|
| 23 |
|
| 24 |
elif "hello" in user_input or "hi" in user_input:
|
| 25 |
+
return "Hello! How can I help you with your studies today?", history
|
| 26 |
|
| 27 |
else:
|
| 28 |
+
return "I'm here to assist with general academic questions. Feel free to ask about study tips, time management, or anything else!", history
|
| 29 |
|
| 30 |
+
# Gradio interface setup
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
gr.Markdown("# Study Assistance Chatbot")
|
| 33 |
gr.Markdown("Welcome! Ask me anything related to your academic studies.")
|
| 34 |
|
| 35 |
+
chatbot = gr.Chatbot() # Chat history UI
|
| 36 |
+
user_input = gr.Textbox(label="Enter your question here:", placeholder="Type your question...") # Textbox for user input
|
| 37 |
+
submit_button = gr.Button("Submit") # Submit button for user input
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Initialize the chat history with a welcome message
|
| 40 |
+
initial_history = [("Chatbot", "Welcome! How can I assist you with your studies today?")]
|
| 41 |
+
|
| 42 |
+
# Submit action - to update chat with response and add it to chat history
|
| 43 |
+
submit_button.click(chatbot_response, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
|
| 44 |
|
| 45 |
# Launch the Gradio app
|
| 46 |
demo.launch()
|
| 47 |
|
|
|