Spaces:
Sleeping
Sleeping
Update llm_app_enhanced.py
#1
by
SreekarB
- opened
- llm_app_enhanced.py +88 -79
llm_app_enhanced.py
CHANGED
|
@@ -70,19 +70,31 @@ class CodePracticeAssistant:
|
|
| 70 |
# Check if we have curriculum content or need to use generic approach
|
| 71 |
if "No specific curriculum content found" in curriculum_content or "could not be retrieved" in curriculum_content:
|
| 72 |
# Use generic prompt without curriculum context
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
prompt = f"""Create a programming practice problem for a student learning {topic}.
|
| 74 |
|
| 75 |
Problem Type: {problem_types.get(internal_type, internal_type)}
|
|
|
|
|
|
|
| 76 |
|
| 77 |
Requirements:
|
| 78 |
-
- Make it appropriate for
|
| 79 |
-
-
|
| 80 |
-
-
|
|
|
|
| 81 |
- If it's a debug problem, include the buggy code
|
| 82 |
- If it's an optimization problem, provide the original code
|
| 83 |
- Make it engaging and educational
|
| 84 |
-
-
|
| 85 |
-
- Focus on what they need to accomplish, not how to do it
|
| 86 |
|
| 87 |
Format your response as:
|
| 88 |
PROBLEM: [The problem description and requirements]
|
|
@@ -98,14 +110,26 @@ Example Output:
|
|
| 98 |
1 2 3 4 5
|
| 99 |
```
|
| 100 |
|
| 101 |
-
Keep it concise but clear."""
|
| 102 |
else:
|
| 103 |
-
# Use curriculum-based prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
prompt = f"""Create a programming practice problem for a student learning {topic}.
|
| 105 |
|
| 106 |
Curriculum Context: {curriculum_content}
|
| 107 |
|
| 108 |
Problem Type: {problem_types.get(internal_type, internal_type)}
|
|
|
|
|
|
|
| 109 |
|
| 110 |
Requirements:
|
| 111 |
- Base the problem on the curriculum content provided above
|
|
@@ -114,12 +138,11 @@ Requirements:
|
|
| 114 |
- Make it appropriate for the skill level shown in the curriculum
|
| 115 |
- Include clear instructions that reference the curriculum concepts
|
| 116 |
- Provide a specific, focused problem that reinforces what they learned
|
|
|
|
| 117 |
- If it's a debug problem, include the buggy code
|
| 118 |
- If it's an optimization problem, provide the original code
|
| 119 |
- Make it engaging and educational
|
| 120 |
-
-
|
| 121 |
-
- Focus on what they need to accomplish, not how to do it
|
| 122 |
-
- Guide them to think through the problem themselves
|
| 123 |
|
| 124 |
Format your response as:
|
| 125 |
PROBLEM: [The problem description and requirements]
|
|
@@ -135,13 +158,13 @@ Example Output:
|
|
| 135 |
1 2 3 4 5
|
| 136 |
```
|
| 137 |
|
| 138 |
-
Keep it concise but clear and aligned with the curriculum difficulty."""
|
| 139 |
|
| 140 |
try:
|
| 141 |
response = self.anthropic_client.messages.create(
|
| 142 |
model="claude-3-5-haiku-20241022",
|
| 143 |
max_tokens=1000,
|
| 144 |
-
temperature=0.
|
| 145 |
messages=[{"role": "user", "content": prompt}]
|
| 146 |
)
|
| 147 |
|
|
@@ -185,7 +208,7 @@ Keep it concise but clear and aligned with the curriculum difficulty."""
|
|
| 185 |
return f"Curriculum content for '{topic}' could not be retrieved."
|
| 186 |
|
| 187 |
def analyze_student_code(self, topic, problem_type, problem_description, student_code):
|
| 188 |
-
"""Analyze student's code and provide
|
| 189 |
if not self.anthropic_client:
|
| 190 |
return "LLM not available. Please check your API key."
|
| 191 |
|
|
@@ -213,7 +236,7 @@ Keep it concise but clear and aligned with the curriculum difficulty."""
|
|
| 213 |
|
| 214 |
# Check if we have curriculum content or need to use generic approach
|
| 215 |
if "No specific curriculum content found" in curriculum_content or "could not be retrieved" in curriculum_content:
|
| 216 |
-
# Use generic analysis prompt
|
| 217 |
prompt = f"""You are a helpful programming tutor. Analyze this student's code and provide step-by-step guidance.
|
| 218 |
|
| 219 |
Problem Type: {problem_type}
|
|
@@ -232,7 +255,7 @@ Provide a CONCISE, step-by-step analysis:
|
|
| 232 |
|
| 233 |
IMPORTANT: Guide them to figure out the solution themselves. Don't provide complete working code unless they're completely stuck. Focus on hints and direction."""
|
| 234 |
else:
|
| 235 |
-
# Use curriculum-based analysis prompt
|
| 236 |
prompt = f"""You are a helpful programming tutor. Analyze this student's code and provide step-by-step guidance.
|
| 237 |
|
| 238 |
Curriculum Context: {curriculum_content}
|
|
@@ -247,16 +270,16 @@ Analysis Type: {analysis_types.get(internal_type, "General analysis")}
|
|
| 247 |
|
| 248 |
Provide a CONCISE, step-by-step analysis:
|
| 249 |
1. **What's Working** (1-2 sentences)
|
| 250 |
-
2. **Areas to Improve** (2-3 specific points
|
| 251 |
3. **Step-by-Step Solution** (guide them through the process, don't give complete code)
|
| 252 |
-
4. **Key Takeaway** (1 sentence
|
| 253 |
|
| 254 |
IMPORTANT: Guide them to figure out the solution themselves. Don't provide complete working code unless they're completely stuck. Focus on hints and direction based on curriculum concepts."""
|
| 255 |
|
| 256 |
try:
|
| 257 |
response = self.anthropic_client.messages.create(
|
| 258 |
model="claude-3-5-haiku-20241022",
|
| 259 |
-
max_tokens=
|
| 260 |
temperature=0.7,
|
| 261 |
messages=[{"role": "user", "content": prompt}]
|
| 262 |
)
|
|
@@ -385,18 +408,21 @@ Thanks! Slide number:"""
|
|
| 385 |
)
|
| 386 |
|
| 387 |
# Create answer generation prompt
|
| 388 |
-
answer_template = """
|
| 389 |
"{question}"
|
| 390 |
Here's what the curriculum slide says about it:
|
| 391 |
{slide_content}
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
-
|
| 395 |
-
-
|
| 396 |
-
-
|
| 397 |
-
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
self.answer_prompt = PromptTemplate(
|
| 402 |
input_variables=["question", "slide_content"],
|
|
@@ -525,7 +551,7 @@ Keep it brief and encouraging!"""
|
|
| 525 |
# Get LLM's answer
|
| 526 |
response = self.anthropic_client.messages.create(
|
| 527 |
model="claude-3-5-haiku-20241022",
|
| 528 |
-
max_tokens=
|
| 529 |
temperature=0.7,
|
| 530 |
messages=[{"role": "user", "content": prompt}]
|
| 531 |
)
|
|
@@ -617,39 +643,36 @@ def extract_topic_from_query(query):
|
|
| 617 |
|
| 618 |
return "programming basics" # Default topic
|
| 619 |
|
| 620 |
-
def
|
| 621 |
-
"""Generate practice problem and return
|
| 622 |
topic = extract_topic_from_query(query)
|
| 623 |
-
# Generate a problem for this topic
|
| 624 |
problem, code = practice_assistant.generate_practice_problem(topic, "Create Practice Problems")
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
font-size: 16px !important;
|
| 642 |
-
color: #666 !important;
|
| 643 |
-
}
|
| 644 |
-
.terminal-output {
|
| 645 |
-
font-family: 'Courier New', monospace !important;
|
| 646 |
-
font-size: 14px !important;
|
| 647 |
-
background-color: #f5f5f5 !important;
|
| 648 |
-
}
|
| 649 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
-
with gr.Blocks(title="Enhanced LLM Curriculum Assistant", theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 652 |
-
gr.Markdown("# π€ Enhanced LLM Curriculum Assistant\nYour AI programming tutor with improved UX and accessibility!")
|
| 653 |
|
| 654 |
with gr.Tabs() as tabs:
|
| 655 |
# Tab 1: Chat Assistant
|
|
@@ -666,7 +689,7 @@ with gr.Blocks(title="Enhanced LLM Curriculum Assistant", theme=gr.themes.Soft()
|
|
| 666 |
lines=3
|
| 667 |
)
|
| 668 |
submit = gr.Button("π€ Ask AI", variant="primary", size="lg")
|
| 669 |
-
answer = gr.Markdown(label="LLM Generated Answer"
|
| 670 |
|
| 671 |
# Practice button
|
| 672 |
practice_btn = gr.Button("π» Practice This Topic", variant="secondary", size="lg", visible=False)
|
|
@@ -725,7 +748,7 @@ with gr.Blocks(title="Enhanced LLM Curriculum Assistant", theme=gr.themes.Soft()
|
|
| 725 |
generate_btn = gr.Button("π² Generate Problem", variant="primary", size="lg")
|
| 726 |
|
| 727 |
gr.Markdown("#### π Problem Description")
|
| 728 |
-
problem_description = gr.Markdown(label="Problem will appear here..."
|
| 729 |
|
| 730 |
gr.Markdown("#### π» Starter Code (if applicable)")
|
| 731 |
starter_code = gr.Code(
|
|
@@ -753,12 +776,11 @@ with gr.Blocks(title="Enhanced LLM Curriculum Assistant", theme=gr.themes.Soft()
|
|
| 753 |
label="Terminal Output",
|
| 754 |
lines=8,
|
| 755 |
value="# Code execution output will appear here...",
|
| 756 |
-
interactive=False
|
| 757 |
-
elem_classes=["terminal-output"]
|
| 758 |
)
|
| 759 |
|
| 760 |
gr.Markdown("#### π AI Analysis")
|
| 761 |
-
analysis_output = gr.Markdown(label="Analysis will appear here..."
|
| 762 |
|
| 763 |
# Event handlers for practice
|
| 764 |
generate_btn.click(
|
|
@@ -773,25 +795,12 @@ with gr.Blocks(title="Enhanced LLM Curriculum Assistant", theme=gr.themes.Soft()
|
|
| 773 |
outputs=[terminal_output, analysis_output]
|
| 774 |
)
|
| 775 |
|
| 776 |
-
# Practice button
|
| 777 |
-
def practice_with_navigation(query):
|
| 778 |
-
topic, problem, code = go_to_practice(query)
|
| 779 |
-
return topic, problem, code
|
| 780 |
-
|
| 781 |
practice_btn.click(
|
| 782 |
-
fn=
|
| 783 |
inputs=[question],
|
| 784 |
-
outputs=[topic_input, problem_description, starter_code]
|
| 785 |
-
)
|
| 786 |
-
|
| 787 |
-
# Add a separate event to switch tabs
|
| 788 |
-
def switch_to_practice():
|
| 789 |
-
return gr.update(selected=1)
|
| 790 |
-
|
| 791 |
-
practice_btn.click(
|
| 792 |
-
fn=switch_to_practice,
|
| 793 |
-
outputs=[tabs]
|
| 794 |
)
|
| 795 |
|
| 796 |
if __name__ == "__main__":
|
| 797 |
-
demo.launch()
|
|
|
|
| 70 |
# Check if we have curriculum content or need to use generic approach
|
| 71 |
if "No specific curriculum content found" in curriculum_content or "could not be retrieved" in curriculum_content:
|
| 72 |
# Use generic prompt without curriculum context
|
| 73 |
+
# Add randomization for diversity
|
| 74 |
+
difficulty_levels = ["beginner", "intermediate", "challenging"]
|
| 75 |
+
problem_styles = [
|
| 76 |
+
"real-world scenario", "mathematical calculation", "data processing",
|
| 77 |
+
"game logic", "text manipulation", "pattern recognition", "algorithm implementation"
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
selected_difficulty = random.choice(difficulty_levels)
|
| 81 |
+
selected_style = random.choice(problem_styles)
|
| 82 |
+
|
| 83 |
prompt = f"""Create a programming practice problem for a student learning {topic}.
|
| 84 |
|
| 85 |
Problem Type: {problem_types.get(internal_type, internal_type)}
|
| 86 |
+
Difficulty: {selected_difficulty}
|
| 87 |
+
Style: {selected_style}
|
| 88 |
|
| 89 |
Requirements:
|
| 90 |
+
- Make it appropriate for {selected_difficulty} level
|
| 91 |
+
- Use a {selected_style} approach
|
| 92 |
+
- Include clear instructions
|
| 93 |
+
- Provide a specific, focused problem
|
| 94 |
- If it's a debug problem, include the buggy code
|
| 95 |
- If it's an optimization problem, provide the original code
|
| 96 |
- Make it engaging and educational
|
| 97 |
+
- Be creative and diverse - avoid repetitive problems
|
|
|
|
| 98 |
|
| 99 |
Format your response as:
|
| 100 |
PROBLEM: [The problem description and requirements]
|
|
|
|
| 110 |
1 2 3 4 5
|
| 111 |
```
|
| 112 |
|
| 113 |
+
Keep it concise but clear and make it different from typical textbook problems."""
|
| 114 |
else:
|
| 115 |
+
# Use curriculum-based prompt with diversity
|
| 116 |
+
# Add randomization for diversity even with curriculum
|
| 117 |
+
difficulty_levels = ["beginner", "intermediate", "challenging"]
|
| 118 |
+
problem_styles = [
|
| 119 |
+
"real-world scenario", "mathematical calculation", "data processing",
|
| 120 |
+
"game logic", "text manipulation", "pattern recognition", "algorithm implementation"
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
selected_difficulty = random.choice(difficulty_levels)
|
| 124 |
+
selected_style = random.choice(problem_styles)
|
| 125 |
+
|
| 126 |
prompt = f"""Create a programming practice problem for a student learning {topic}.
|
| 127 |
|
| 128 |
Curriculum Context: {curriculum_content}
|
| 129 |
|
| 130 |
Problem Type: {problem_types.get(internal_type, internal_type)}
|
| 131 |
+
Difficulty: {selected_difficulty}
|
| 132 |
+
Style: {selected_style}
|
| 133 |
|
| 134 |
Requirements:
|
| 135 |
- Base the problem on the curriculum content provided above
|
|
|
|
| 138 |
- Make it appropriate for the skill level shown in the curriculum
|
| 139 |
- Include clear instructions that reference the curriculum concepts
|
| 140 |
- Provide a specific, focused problem that reinforces what they learned
|
| 141 |
+
- Use a {selected_style} approach to make it diverse
|
| 142 |
- If it's a debug problem, include the buggy code
|
| 143 |
- If it's an optimization problem, provide the original code
|
| 144 |
- Make it engaging and educational
|
| 145 |
+
- Be creative and diverse - avoid repetitive problems
|
|
|
|
|
|
|
| 146 |
|
| 147 |
Format your response as:
|
| 148 |
PROBLEM: [The problem description and requirements]
|
|
|
|
| 158 |
1 2 3 4 5
|
| 159 |
```
|
| 160 |
|
| 161 |
+
Keep it concise but clear and aligned with the curriculum difficulty while being creative and diverse."""
|
| 162 |
|
| 163 |
try:
|
| 164 |
response = self.anthropic_client.messages.create(
|
| 165 |
model="claude-3-5-haiku-20241022",
|
| 166 |
max_tokens=1000,
|
| 167 |
+
temperature=0.9, # Higher temperature for more diversity
|
| 168 |
messages=[{"role": "user", "content": prompt}]
|
| 169 |
)
|
| 170 |
|
|
|
|
| 208 |
return f"Curriculum content for '{topic}' could not be retrieved."
|
| 209 |
|
| 210 |
def analyze_student_code(self, topic, problem_type, problem_description, student_code):
|
| 211 |
+
"""Analyze student's code and provide feedback based on curriculum"""
|
| 212 |
if not self.anthropic_client:
|
| 213 |
return "LLM not available. Please check your API key."
|
| 214 |
|
|
|
|
| 236 |
|
| 237 |
# Check if we have curriculum content or need to use generic approach
|
| 238 |
if "No specific curriculum content found" in curriculum_content or "could not be retrieved" in curriculum_content:
|
| 239 |
+
# Use generic analysis prompt - Tutor style
|
| 240 |
prompt = f"""You are a helpful programming tutor. Analyze this student's code and provide step-by-step guidance.
|
| 241 |
|
| 242 |
Problem Type: {problem_type}
|
|
|
|
| 255 |
|
| 256 |
IMPORTANT: Guide them to figure out the solution themselves. Don't provide complete working code unless they're completely stuck. Focus on hints and direction."""
|
| 257 |
else:
|
| 258 |
+
# Use curriculum-based analysis prompt - Tutor style
|
| 259 |
prompt = f"""You are a helpful programming tutor. Analyze this student's code and provide step-by-step guidance.
|
| 260 |
|
| 261 |
Curriculum Context: {curriculum_content}
|
|
|
|
| 270 |
|
| 271 |
Provide a CONCISE, step-by-step analysis:
|
| 272 |
1. **What's Working** (1-2 sentences)
|
| 273 |
+
2. **Areas to Improve** (2-3 specific points)
|
| 274 |
3. **Step-by-Step Solution** (guide them through the process, don't give complete code)
|
| 275 |
+
4. **Key Takeaway** (1 sentence)
|
| 276 |
|
| 277 |
IMPORTANT: Guide them to figure out the solution themselves. Don't provide complete working code unless they're completely stuck. Focus on hints and direction based on curriculum concepts."""
|
| 278 |
|
| 279 |
try:
|
| 280 |
response = self.anthropic_client.messages.create(
|
| 281 |
model="claude-3-5-haiku-20241022",
|
| 282 |
+
max_tokens=1500,
|
| 283 |
temperature=0.7,
|
| 284 |
messages=[{"role": "user", "content": prompt}]
|
| 285 |
)
|
|
|
|
| 408 |
)
|
| 409 |
|
| 410 |
# Create answer generation prompt
|
| 411 |
+
answer_template = """Hey there! I'm helping a student understand a programming concept. They asked:
|
| 412 |
"{question}"
|
| 413 |
Here's what the curriculum slide says about it:
|
| 414 |
{slide_content}
|
| 415 |
+
Could you help me explain this to them in a friendly, educational way? I'd like you to:
|
| 416 |
+
- Break it down in simple terms
|
| 417 |
+
- Use examples if the slide has them
|
| 418 |
+
- Make it step-by-step and easy to follow
|
| 419 |
+
- Add some helpful context if the slide is brief
|
| 420 |
+
- Use bullet points or lists to make it clear
|
| 421 |
+
- Make sure your answer directly addresses what they asked
|
| 422 |
+
- Include code examples when relevant
|
| 423 |
+
- Provide detailed explanations with examples
|
| 424 |
+
- Be comprehensive and educational
|
| 425 |
+
Thanks for your help! Here's what I'd tell the student:"""
|
| 426 |
|
| 427 |
self.answer_prompt = PromptTemplate(
|
| 428 |
input_variables=["question", "slide_content"],
|
|
|
|
| 551 |
# Get LLM's answer
|
| 552 |
response = self.anthropic_client.messages.create(
|
| 553 |
model="claude-3-5-haiku-20241022",
|
| 554 |
+
max_tokens=2000,
|
| 555 |
temperature=0.7,
|
| 556 |
messages=[{"role": "user", "content": prompt}]
|
| 557 |
)
|
|
|
|
| 643 |
|
| 644 |
return "programming basics" # Default topic
|
| 645 |
|
| 646 |
+
def generate_practice_for_topic(query):
|
| 647 |
+
"""Generate practice problem and return with navigation message"""
|
| 648 |
topic = extract_topic_from_query(query)
|
|
|
|
| 649 |
problem, code = practice_assistant.generate_practice_problem(topic, "Create Practice Problems")
|
| 650 |
+
|
| 651 |
+
# Create a message to guide user to practice tab
|
| 652 |
+
navigation_message = f"""
|
| 653 |
+
π― **Practice Problem Generated!**
|
| 654 |
+
|
| 655 |
+
I've created a practice problem for **{topic}** and loaded it into the Code Practice tab.
|
| 656 |
+
|
| 657 |
+
**To continue practicing:**
|
| 658 |
+
1. Click on the **"π» Code Practice"** tab above
|
| 659 |
+
2. You'll see the topic and problem already filled in
|
| 660 |
+
3. Write your solution and click "Analyze My Code"
|
| 661 |
+
|
| 662 |
+
**Topic:** {topic}
|
| 663 |
+
**Problem Type:** Create Practice Problems
|
| 664 |
+
|
| 665 |
+
Ready to practice? Switch to the Code Practice tab! π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
"""
|
| 667 |
+
|
| 668 |
+
return navigation_message, topic, problem, code
|
| 669 |
+
|
| 670 |
+
|
| 671 |
+
|
| 672 |
+
with gr.Blocks(title="LLM Curriculum Assistant", theme=gr.themes.Soft()) as demo:
|
| 673 |
+
gr.Markdown("# π€ LLM Curriculum Assistant\nYour AI programming tutor with LLM-powered content selection and code practice!")
|
| 674 |
+
|
| 675 |
|
|
|
|
|
|
|
| 676 |
|
| 677 |
with gr.Tabs() as tabs:
|
| 678 |
# Tab 1: Chat Assistant
|
|
|
|
| 689 |
lines=3
|
| 690 |
)
|
| 691 |
submit = gr.Button("π€ Ask AI", variant="primary", size="lg")
|
| 692 |
+
answer = gr.Markdown(label="LLM Generated Answer")
|
| 693 |
|
| 694 |
# Practice button
|
| 695 |
practice_btn = gr.Button("π» Practice This Topic", variant="secondary", size="lg", visible=False)
|
|
|
|
| 748 |
generate_btn = gr.Button("π² Generate Problem", variant="primary", size="lg")
|
| 749 |
|
| 750 |
gr.Markdown("#### π Problem Description")
|
| 751 |
+
problem_description = gr.Markdown(label="Problem will appear here...")
|
| 752 |
|
| 753 |
gr.Markdown("#### π» Starter Code (if applicable)")
|
| 754 |
starter_code = gr.Code(
|
|
|
|
| 776 |
label="Terminal Output",
|
| 777 |
lines=8,
|
| 778 |
value="# Code execution output will appear here...",
|
| 779 |
+
interactive=False
|
|
|
|
| 780 |
)
|
| 781 |
|
| 782 |
gr.Markdown("#### π AI Analysis")
|
| 783 |
+
analysis_output = gr.Markdown(label="Analysis will appear here...")
|
| 784 |
|
| 785 |
# Event handlers for practice
|
| 786 |
generate_btn.click(
|
|
|
|
| 795 |
outputs=[terminal_output, analysis_output]
|
| 796 |
)
|
| 797 |
|
| 798 |
+
# Practice button - generate problem and show navigation message
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
practice_btn.click(
|
| 800 |
+
fn=generate_practice_for_topic,
|
| 801 |
inputs=[question],
|
| 802 |
+
outputs=[answer, topic_input, problem_description, starter_code]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
)
|
| 804 |
|
| 805 |
if __name__ == "__main__":
|
| 806 |
+
demo.launch()
|