Zheng Qu
commited on
Commit
•
e720bf1
1
Parent(s):
11c72a0
add chatClaude.py
Browse files- app.py +1 -1
- chatClaude.py +145 -0
- grading.py +1 -1
app.py
CHANGED
@@ -106,4 +106,4 @@ def create_app():
|
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
app = create_app()
|
109 |
-
app.launch(share=True)
|
|
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
app = create_app()
|
109 |
+
app.launch(share=True, reload_exclude=["MIDTERM*/*", "*.db"])
|
chatClaude.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
from typing import List, Dict
|
4 |
+
import anthropic
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
class ClaudeChat:
|
8 |
+
def __init__(self, max_history: int = 10):
|
9 |
+
"""
|
10 |
+
Initialize Claude chat client with history management
|
11 |
+
|
12 |
+
Args:
|
13 |
+
max_history (int): Maximum number of messages to keep in history
|
14 |
+
"""
|
15 |
+
# Load environment variables
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
# Retrieve API key
|
19 |
+
self.api_key = os.getenv('ClaudeAI_API_KEY')
|
20 |
+
if not self.api_key:
|
21 |
+
print("Error: ClaudeAI_API_KEY not found in environment variables")
|
22 |
+
sys.exit(1)
|
23 |
+
|
24 |
+
# Initialize client
|
25 |
+
self.client = anthropic.Anthropic(api_key=self.api_key)
|
26 |
+
|
27 |
+
# Conversation history
|
28 |
+
self.history: List[Dict] = []
|
29 |
+
self.max_history = max_history
|
30 |
+
|
31 |
+
# Model selection
|
32 |
+
self.model = "claude-3-5-sonnet-latest"
|
33 |
+
|
34 |
+
# System prompt
|
35 |
+
self.system_prompt = """You are an expert computer science and data science educator. Your task is to create clear, engaging, and pedagogically sound lecture slides. Follow these guidelines:
|
36 |
+
|
37 |
+
1. Structure:
|
38 |
+
- Start with learning objectives
|
39 |
+
- Present concepts progressively from simple to complex
|
40 |
+
- Include concrete examples and visualizations
|
41 |
+
- End with summary/key takeaways
|
42 |
+
|
43 |
+
2. Content:
|
44 |
+
- Use clear, concise language
|
45 |
+
- Include practical examples and code snippets
|
46 |
+
- Add visualizations wherever possible
|
47 |
+
- Highlight key concepts and terminology
|
48 |
+
|
49 |
+
3. Format:
|
50 |
+
- Use consistent slide templates
|
51 |
+
- Include code chunks with proper formatting
|
52 |
+
- Hide implementation details when appropriate
|
53 |
+
- Use color coding for important concepts
|
54 |
+
|
55 |
+
4. Examples:
|
56 |
+
- Start with small, tractable examples
|
57 |
+
- Show step-by-step problem solving
|
58 |
+
- Include both successful and error cases
|
59 |
+
- Connect to real-world applications
|
60 |
+
|
61 |
+
5. Technical details:
|
62 |
+
- Use #| echo: false for hidden code chunks
|
63 |
+
- use R code to generate plots/graphs where necessary, R generate image files to be embedded in the slides
|
64 |
+
- R code for generating plots/graphs should be embedded in slides but hidden
|
65 |
+
- Use latex for tables when necessary
|
66 |
+
- use "$" for inline latex math/expression and "$$" for block latex
|
67 |
+
- Include proper figure dimensions
|
68 |
+
- Ensure code outputs are readable
|
69 |
+
- Test all code examples before including
|
70 |
+
- output quarto beamer slide deck.
|
71 |
+
|
72 |
+
Provide clear, educational content suitable for undergraduate/graduate level computer science and data science courses."""
|
73 |
+
self.add_to_history("system", self.system_prompt)
|
74 |
+
|
75 |
+
def add_to_history(self, role: str, content: str):
|
76 |
+
"""
|
77 |
+
Add message to conversation history
|
78 |
+
|
79 |
+
Args:
|
80 |
+
role (str): 'user' or 'assistant'
|
81 |
+
content (str): Message content
|
82 |
+
"""
|
83 |
+
self.history.append({"role": role, "content": content})
|
84 |
+
|
85 |
+
# Trim history if it exceeds max_history
|
86 |
+
if len(self.history) > self.max_history:
|
87 |
+
self.history.pop(0)
|
88 |
+
|
89 |
+
def chat(self):
|
90 |
+
"""
|
91 |
+
Interactive chat loop with Claude
|
92 |
+
"""
|
93 |
+
print(f"🤖 Claude Chat (Model: {self.model})")
|
94 |
+
print("Type 'exit' or 'quit' to end the conversation.")
|
95 |
+
print("Type 'clear' to reset conversation history.\n")
|
96 |
+
|
97 |
+
while True:
|
98 |
+
try:
|
99 |
+
# Get user input
|
100 |
+
user_input = input("You: ").strip()
|
101 |
+
|
102 |
+
# Handle special commands
|
103 |
+
if user_input.lower() in ['exit', 'quit']:
|
104 |
+
print("Goodbye!")
|
105 |
+
break
|
106 |
+
|
107 |
+
if user_input.lower() == 'clear':
|
108 |
+
self.history.clear()
|
109 |
+
print("Conversation history cleared.")
|
110 |
+
continue
|
111 |
+
|
112 |
+
# Add user message to history
|
113 |
+
self.add_to_history("user", user_input)
|
114 |
+
|
115 |
+
# Prepare messages for API call
|
116 |
+
messages = [
|
117 |
+
{"role": msg["role"], "content": msg["content"]}
|
118 |
+
for msg in self.history
|
119 |
+
]
|
120 |
+
|
121 |
+
# Create message with Claude
|
122 |
+
response = self.client.messages.create(
|
123 |
+
model=self.model,
|
124 |
+
max_tokens=14096,
|
125 |
+
messages=messages
|
126 |
+
)
|
127 |
+
|
128 |
+
# Extract and print Claude's response
|
129 |
+
claude_response = response.content[0].text
|
130 |
+
print(f"\nClaude: {claude_response}\n")
|
131 |
+
|
132 |
+
# Add assistant response to history
|
133 |
+
self.add_to_history("assistant", claude_response)
|
134 |
+
|
135 |
+
except KeyboardInterrupt:
|
136 |
+
print("\nChat interrupted. Type 'exit' to quit.")
|
137 |
+
except Exception as e:
|
138 |
+
print(f"An error occurred: {e}")
|
139 |
+
|
140 |
+
def main():
|
141 |
+
chat_client = ClaudeChat()
|
142 |
+
chat_client.chat()
|
143 |
+
|
144 |
+
if __name__ == "__main__":
|
145 |
+
main()
|
grading.py
CHANGED
@@ -73,7 +73,7 @@ def generate_markdown_report(student_submissions: Dict[str, Dict[str, dict]], ou
|
|
73 |
|
74 |
for student_name, problems in student_submissions.items():
|
75 |
# Create filename from student name
|
76 |
-
filename = f"{student_name.replace(' ', '_')}.qmd"
|
77 |
filepath = os.path.join(output_dir, filename)
|
78 |
|
79 |
header = f"""---
|
|
|
73 |
|
74 |
for student_name, problems in student_submissions.items():
|
75 |
# Create filename from student name
|
76 |
+
filename = f"{student_name.replace(' ', '_')}_{SESSION_ID}.qmd"
|
77 |
filepath = os.path.join(output_dir, filename)
|
78 |
|
79 |
header = f"""---
|