Spaces:
Runtime error
Runtime error
abhilashnl2006
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -52,4 +52,72 @@ def check_answer(topic, user_code):
|
|
52 |
feedback = response.choices[0].message['content'].strip()
|
53 |
return feedback
|
54 |
except Exception as e:
|
55 |
-
return f"Error: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
feedback = response.choices[0].message['content'].strip()
|
53 |
return feedback
|
54 |
except Exception as e:
|
55 |
+
return f"Error: {str(e)}"
|
56 |
+
|
57 |
+
def respond(message, chat_history):
|
58 |
+
if chat_history is None:
|
59 |
+
chat_history = []
|
60 |
+
|
61 |
+
# Check if this is the first message to set the topic
|
62 |
+
if len(chat_history) == 0:
|
63 |
+
topic = message
|
64 |
+
question = generate_exercise_question(topic)
|
65 |
+
chat_history.append((message, question))
|
66 |
+
return "", chat_history, chat_history, topic, None
|
67 |
+
else:
|
68 |
+
chat_history.append((message, "Please answer the question or type 'next' for another question on the same topic."))
|
69 |
+
return "", chat_history, chat_history, "", None
|
70 |
+
|
71 |
+
def submit_code(topic, user_code, chat_history):
|
72 |
+
feedback = check_answer(topic, user_code)
|
73 |
+
chat_history.append((user_code, feedback))
|
74 |
+
return "", chat_history, chat_history, "", None
|
75 |
+
|
76 |
+
def undo(chat_history):
|
77 |
+
if chat_history:
|
78 |
+
chat_history.pop()
|
79 |
+
return chat_history
|
80 |
+
|
81 |
+
def clear():
|
82 |
+
return [], [], "", ""
|
83 |
+
|
84 |
+
# Create the Gradio interface
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("# Python Tutor")
|
87 |
+
gr.Markdown("Helps you learn Python using the latest documentation.")
|
88 |
+
|
89 |
+
chatbot = gr.Chatbot(label="Python Tutor")
|
90 |
+
state = gr.State([])
|
91 |
+
topic_state = gr.State("")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
user_input = gr.Textbox(
|
96 |
+
show_label=False,
|
97 |
+
placeholder="Type the Python topic you want to learn about...",
|
98 |
+
lines=1
|
99 |
+
)
|
100 |
+
submit_button = gr.Button("Submit")
|
101 |
+
code_input = gr.Code(language="python", lines=10)
|
102 |
+
code_submit_button = gr.Button("Submit Code")
|
103 |
+
undo_button = gr.Button("Undo")
|
104 |
+
clear_button = gr.Button("Clear")
|
105 |
+
|
106 |
+
submit_button.click(respond, [user_input, state], [user_input, state, chatbot, topic_state])
|
107 |
+
code_submit_button.click(submit_code, [topic_state, code_input, state], [code_input, state, chatbot, topic_state])
|
108 |
+
undo_button.click(undo, state, chatbot)
|
109 |
+
clear_button.click(clear, None, [chatbot, state, topic_state])
|
110 |
+
|
111 |
+
examples = [
|
112 |
+
["lists"],
|
113 |
+
["decorators"],
|
114 |
+
["dictionaries"],
|
115 |
+
["file handling"],
|
116 |
+
["classes and objects"]
|
117 |
+
]
|
118 |
+
|
119 |
+
# Add examples
|
120 |
+
gr.Examples(examples=examples, inputs=[user_input])
|
121 |
+
|
122 |
+
# Launch the app
|
123 |
+
demo.launch()
|