Spaces:
Runtime error
Runtime error
abhilashnl2006
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -67,77 +67,72 @@ def get_solution(topic, exercise):
|
|
67 |
except Exception as e:
|
68 |
return f"Error: {str(e)}"
|
69 |
|
70 |
-
def
|
71 |
if not chat_history:
|
72 |
-
topic
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
else:
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
feedback = check_answer(topic, exercise, user_code)
|
88 |
-
|
89 |
-
if "correct" in feedback.lower():
|
90 |
-
response = f"{feedback}\n\nGreat job! Type 'next' to see the next question."
|
91 |
else:
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
chat_history.append((user_code, response))
|
96 |
-
return "", chat_history, gr.update(visible=True), gr.update(visible=True)
|
97 |
|
98 |
-
def undo(chat_history):
|
99 |
if chat_history:
|
100 |
chat_history.pop()
|
101 |
-
return chat_history
|
102 |
|
103 |
def clear():
|
104 |
-
return [], "", gr.update(visible=False
|
105 |
|
106 |
with gr.Blocks() as demo:
|
107 |
gr.Markdown("# Python Practice Bot")
|
108 |
gr.Markdown("Learn Python through exercises. Start by entering a topic you want to practice.")
|
109 |
|
110 |
chatbot = gr.Chatbot(label="Python Tutor")
|
111 |
-
|
112 |
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=3):
|
115 |
user_input = gr.Textbox(
|
116 |
show_label=False,
|
117 |
-
placeholder="Enter a Python topic
|
118 |
lines=1
|
119 |
)
|
120 |
code_input = gr.Code(language="python", label="Your Solution", visible=False)
|
121 |
with gr.Column(scale=1):
|
122 |
submit_button = gr.Button("Submit")
|
123 |
-
code_submit_button = gr.Button("Submit Code", visible=False)
|
124 |
undo_button = gr.Button("Undo")
|
125 |
clear_button = gr.Button("Clear")
|
126 |
|
127 |
submit_button.click(
|
128 |
-
|
129 |
-
[user_input, chatbot,
|
130 |
-
[user_input, chatbot,
|
131 |
-
)
|
132 |
-
|
133 |
-
code_submit_button.click(
|
134 |
-
submit_code,
|
135 |
-
[topic, code_input, chatbot],
|
136 |
-
[code_input, chatbot, code_input, code_submit_button]
|
137 |
)
|
138 |
|
139 |
-
undo_button.click(undo, [chatbot], [chatbot])
|
140 |
-
clear_button.click(clear, None, [chatbot,
|
141 |
|
142 |
gr.Examples(
|
143 |
examples=[["lists"], ["decorators"], ["dictionaries"], ["file handling"], ["classes and objects"]],
|
|
|
67 |
except Exception as e:
|
68 |
return f"Error: {str(e)}"
|
69 |
|
70 |
+
def process_input(user_input, code_input, chat_history, current_topic):
|
71 |
if not chat_history:
|
72 |
+
# First interaction: set topic and generate question
|
73 |
+
current_topic = user_input
|
74 |
+
question = generate_exercise_question(current_topic)
|
75 |
+
chat_history.append((user_input, question))
|
76 |
+
return "", chat_history, current_topic, gr.update(visible=True, value="")
|
77 |
+
elif user_input.lower() == "next":
|
78 |
+
# Generate new question for the same topic
|
79 |
+
question = generate_exercise_question(current_topic)
|
80 |
+
chat_history.append((user_input, question))
|
81 |
+
return "", chat_history, current_topic, gr.update(visible=True, value="")
|
82 |
+
elif code_input.strip():
|
83 |
+
# User submitted code
|
84 |
+
exercise = chat_history[-1][1] # Get the last exercise question
|
85 |
+
feedback = check_answer(current_topic, exercise, code_input)
|
86 |
+
|
87 |
+
if "correct" in feedback.lower():
|
88 |
+
response = f"{feedback}\n\nGreat job! Type 'next' to see the next question."
|
89 |
else:
|
90 |
+
solution = get_solution(current_topic, exercise)
|
91 |
+
response = f"{feedback}\n\nHere's the correct solution:\n\n{solution}\n\nType 'next' to see the next question."
|
92 |
+
|
93 |
+
chat_history.append((code_input, response))
|
94 |
+
return "", chat_history, current_topic, gr.update(visible=True, value="")
|
|
|
|
|
|
|
|
|
95 |
else:
|
96 |
+
# Invalid input
|
97 |
+
chat_history.append((user_input, "Please enter your code in the code editor or type 'next' for a new question."))
|
98 |
+
return "", chat_history, current_topic, gr.update(visible=True)
|
|
|
|
|
99 |
|
100 |
+
def undo(chat_history, current_topic):
|
101 |
if chat_history:
|
102 |
chat_history.pop()
|
103 |
+
return chat_history, current_topic, gr.update(visible=bool(chat_history))
|
104 |
|
105 |
def clear():
|
106 |
+
return [], "", gr.update(visible=False, value="")
|
107 |
|
108 |
with gr.Blocks() as demo:
|
109 |
gr.Markdown("# Python Practice Bot")
|
110 |
gr.Markdown("Learn Python through exercises. Start by entering a topic you want to practice.")
|
111 |
|
112 |
chatbot = gr.Chatbot(label="Python Tutor")
|
113 |
+
current_topic = gr.State("")
|
114 |
|
115 |
with gr.Row():
|
116 |
with gr.Column(scale=3):
|
117 |
user_input = gr.Textbox(
|
118 |
show_label=False,
|
119 |
+
placeholder="Enter a Python topic, type 'next' for a new question, or submit your code...",
|
120 |
lines=1
|
121 |
)
|
122 |
code_input = gr.Code(language="python", label="Your Solution", visible=False)
|
123 |
with gr.Column(scale=1):
|
124 |
submit_button = gr.Button("Submit")
|
|
|
125 |
undo_button = gr.Button("Undo")
|
126 |
clear_button = gr.Button("Clear")
|
127 |
|
128 |
submit_button.click(
|
129 |
+
process_input,
|
130 |
+
[user_input, code_input, chatbot, current_topic],
|
131 |
+
[user_input, chatbot, current_topic, code_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
)
|
133 |
|
134 |
+
undo_button.click(undo, [chatbot, current_topic], [chatbot, current_topic, code_input])
|
135 |
+
clear_button.click(clear, None, [chatbot, current_topic, code_input])
|
136 |
|
137 |
gr.Examples(
|
138 |
examples=[["lists"], ["decorators"], ["dictionaries"], ["file handling"], ["classes and objects"]],
|