Spaces:
Runtime error
Runtime error
Update lm_trial_app.py
Browse files- lm_trial_app.py +77 -4
lm_trial_app.py
CHANGED
|
@@ -86,6 +86,16 @@ Format your response as:
|
|
| 86 |
PROBLEM: [The problem description and requirements]
|
| 87 |
CODE: [Any starter code if applicable, or "Write your code here:"]
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
Keep it concise but clear."""
|
| 90 |
else:
|
| 91 |
# Use curriculum-based prompt
|
|
@@ -110,6 +120,16 @@ Format your response as:
|
|
| 110 |
PROBLEM: [The problem description and requirements]
|
| 111 |
CODE: [Any starter code if applicable, or "Write your code here:"]
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
Keep it concise but clear and aligned with the curriculum difficulty."""
|
| 114 |
|
| 115 |
try:
|
|
@@ -244,6 +264,40 @@ Be encouraging but honest. Focus on learning and improvement based on the curric
|
|
| 244 |
except Exception as e:
|
| 245 |
return f"Error analyzing code: {str(e)}"
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
# --- LLM-Powered Curriculum Assistant ---
|
| 248 |
|
| 249 |
class LLMCurriculumAssistant:
|
|
@@ -534,9 +588,20 @@ def generate_problem(topic, problem_type):
|
|
| 534 |
return problem, code
|
| 535 |
|
| 536 |
def analyze_code(topic, problem_type, problem_description, student_code):
|
| 537 |
-
"""Analyze student's code"""
|
| 538 |
-
|
| 539 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
with gr.Blocks(title="LLM Curriculum Assistant", theme=gr.themes.Soft()) as demo:
|
| 542 |
gr.Markdown("# π€ LLM Curriculum Assistant\nYour AI programming tutor with LLM-powered content selection and code practice!")
|
|
@@ -628,6 +693,14 @@ with gr.Blocks(title="LLM Curriculum Assistant", theme=gr.themes.Soft()) as demo
|
|
| 628 |
|
| 629 |
analyze_btn = gr.Button("π Analyze My Code", variant="secondary", size="lg")
|
| 630 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
gr.Markdown("#### π AI Analysis")
|
| 632 |
analysis_output = gr.Markdown(label="Analysis will appear here...")
|
| 633 |
|
|
@@ -641,7 +714,7 @@ with gr.Blocks(title="LLM Curriculum Assistant", theme=gr.themes.Soft()) as demo
|
|
| 641 |
analyze_btn.click(
|
| 642 |
fn=analyze_code,
|
| 643 |
inputs=[topic_input, problem_type, problem_description, student_code],
|
| 644 |
-
outputs=[analysis_output]
|
| 645 |
)
|
| 646 |
|
| 647 |
if __name__ == "__main__":
|
|
|
|
| 86 |
PROBLEM: [The problem description and requirements]
|
| 87 |
CODE: [Any starter code if applicable, or "Write your code here:"]
|
| 88 |
|
| 89 |
+
IMPORTANT: If you include example outputs, format them as code blocks like this:
|
| 90 |
+
```
|
| 91 |
+
Example Output:
|
| 92 |
+
1
|
| 93 |
+
1 2
|
| 94 |
+
1 2 3
|
| 95 |
+
1 2 3 4
|
| 96 |
+
1 2 3 4 5
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
Keep it concise but clear."""
|
| 100 |
else:
|
| 101 |
# Use curriculum-based prompt
|
|
|
|
| 120 |
PROBLEM: [The problem description and requirements]
|
| 121 |
CODE: [Any starter code if applicable, or "Write your code here:"]
|
| 122 |
|
| 123 |
+
IMPORTANT: If you include example outputs, format them as code blocks like this:
|
| 124 |
+
```
|
| 125 |
+
Example Output:
|
| 126 |
+
1
|
| 127 |
+
1 2
|
| 128 |
+
1 2 3
|
| 129 |
+
1 2 3 4
|
| 130 |
+
1 2 3 4 5
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
Keep it concise but clear and aligned with the curriculum difficulty."""
|
| 134 |
|
| 135 |
try:
|
|
|
|
| 264 |
except Exception as e:
|
| 265 |
return f"Error analyzing code: {str(e)}"
|
| 266 |
|
| 267 |
+
def execute_code(self, code):
|
| 268 |
+
"""Execute the student's code and return the output"""
|
| 269 |
+
try:
|
| 270 |
+
import subprocess
|
| 271 |
+
import sys
|
| 272 |
+
from io import StringIO
|
| 273 |
+
import contextlib
|
| 274 |
+
|
| 275 |
+
# Create a safe execution environment
|
| 276 |
+
local_vars = {}
|
| 277 |
+
output_buffer = StringIO()
|
| 278 |
+
|
| 279 |
+
# Capture stdout and stderr
|
| 280 |
+
with contextlib.redirect_stdout(output_buffer), contextlib.redirect_stderr(output_buffer):
|
| 281 |
+
try:
|
| 282 |
+
# Execute the code
|
| 283 |
+
exec(code, {"__builtins__": __builtins__}, local_vars)
|
| 284 |
+
output = output_buffer.getvalue()
|
| 285 |
+
|
| 286 |
+
if output.strip():
|
| 287 |
+
return f"$ python code.py\n{output}"
|
| 288 |
+
else:
|
| 289 |
+
return f"$ python code.py\n# Code executed successfully but no output produced"
|
| 290 |
+
|
| 291 |
+
except Exception as e:
|
| 292 |
+
error_output = output_buffer.getvalue()
|
| 293 |
+
if error_output.strip():
|
| 294 |
+
return f"$ python code.py\n{error_output}\nError: {str(e)}"
|
| 295 |
+
else:
|
| 296 |
+
return f"$ python code.py\nError: {str(e)}"
|
| 297 |
+
|
| 298 |
+
except Exception as e:
|
| 299 |
+
return f"$ python code.py\nError: Could not execute code - {str(e)}"
|
| 300 |
+
|
| 301 |
# --- LLM-Powered Curriculum Assistant ---
|
| 302 |
|
| 303 |
class LLMCurriculumAssistant:
|
|
|
|
| 588 |
return problem, code
|
| 589 |
|
| 590 |
def analyze_code(topic, problem_type, problem_description, student_code):
|
| 591 |
+
"""Analyze student's code and execute it"""
|
| 592 |
+
try:
|
| 593 |
+
# Execute the code to get terminal output
|
| 594 |
+
terminal_output = practice_assistant.execute_code(student_code)
|
| 595 |
+
except Exception as e:
|
| 596 |
+
terminal_output = f"$ python code.py\nError: Could not execute code - {str(e)}"
|
| 597 |
+
|
| 598 |
+
try:
|
| 599 |
+
# Get LLM analysis (this should always run regardless of execution errors)
|
| 600 |
+
analysis = practice_assistant.analyze_student_code(topic, problem_type, problem_description, student_code)
|
| 601 |
+
except Exception as e:
|
| 602 |
+
analysis = f"Error getting LLM analysis: {str(e)}"
|
| 603 |
+
|
| 604 |
+
return terminal_output, analysis
|
| 605 |
|
| 606 |
with gr.Blocks(title="LLM Curriculum Assistant", theme=gr.themes.Soft()) as demo:
|
| 607 |
gr.Markdown("# π€ LLM Curriculum Assistant\nYour AI programming tutor with LLM-powered content selection and code practice!")
|
|
|
|
| 693 |
|
| 694 |
analyze_btn = gr.Button("π Analyze My Code", variant="secondary", size="lg")
|
| 695 |
|
| 696 |
+
gr.Markdown("#### π» Code Execution Output")
|
| 697 |
+
terminal_output = gr.Code(
|
| 698 |
+
label="Terminal Output",
|
| 699 |
+
language="bash",
|
| 700 |
+
lines=8,
|
| 701 |
+
value="# Code execution output will appear here..."
|
| 702 |
+
)
|
| 703 |
+
|
| 704 |
gr.Markdown("#### π AI Analysis")
|
| 705 |
analysis_output = gr.Markdown(label="Analysis will appear here...")
|
| 706 |
|
|
|
|
| 714 |
analyze_btn.click(
|
| 715 |
fn=analyze_code,
|
| 716 |
inputs=[topic_input, problem_type, problem_description, student_code],
|
| 717 |
+
outputs=[terminal_output, analysis_output]
|
| 718 |
)
|
| 719 |
|
| 720 |
if __name__ == "__main__":
|