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