Spaces:
Build error
Build error
Create src/serci
Browse files
src/serci
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from typing import Dict
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import anthropic
|
| 5 |
+
from src.utils.session import get_tutor_context
|
| 6 |
+
from src.utils.config import get_anthropic_api_key
|
| 7 |
+
|
| 8 |
+
class AITutorService:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.initialize_client()
|
| 11 |
+
self.load_avatar_assets()
|
| 12 |
+
|
| 13 |
+
def initialize_client(self):
|
| 14 |
+
"""Initialize Anthropic client"""
|
| 15 |
+
self.client = anthropic.Anthropic(api_key=get_anthropic_api_key())
|
| 16 |
+
|
| 17 |
+
def load_avatar_assets(self):
|
| 18 |
+
"""Load avatar assets and animations"""
|
| 19 |
+
self.avatar_states = {
|
| 20 |
+
'neutral': """
|
| 21 |
+
<svg width="100" height="100" viewBox="0 0 100 100">
|
| 22 |
+
<circle cx="50" cy="50" r="45" fill="#4A90E2"/>
|
| 23 |
+
<circle cx="35" cy="40" r="5" fill="white"/>
|
| 24 |
+
<circle cx="65" cy="40" r="5" fill="white"/>
|
| 25 |
+
<path d="M 30 60 Q 50 70 70 60" stroke="white" fill="none" stroke-width="3"/>
|
| 26 |
+
</svg>
|
| 27 |
+
""",
|
| 28 |
+
'thinking': """
|
| 29 |
+
<svg width="100" height="100" viewBox="0 0 100 100">
|
| 30 |
+
<circle cx="50" cy="50" r="45" fill="#4A90E2"/>
|
| 31 |
+
<circle cx="35" cy="40" r="5" fill="white"/>
|
| 32 |
+
<circle cx="65" cy="40" r="5" fill="white"/>
|
| 33 |
+
<path d="M 30 65 Q 50 65 70 65" stroke="white" fill="none" stroke-width="3"/>
|
| 34 |
+
</svg>
|
| 35 |
+
""",
|
| 36 |
+
'happy': """
|
| 37 |
+
<svg width="100" height="100" viewBox="0 0 100 100">
|
| 38 |
+
<circle cx="50" cy="50" r="45" fill="#4A90E2"/>
|
| 39 |
+
<circle cx="35" cy="40" r="5" fill="white"/>
|
| 40 |
+
<circle cx="65" cy="40" r="5" fill="white"/>
|
| 41 |
+
<path d="M 30 60 Q 50 80 70 60" stroke="white" fill="none" stroke-width="3"/>
|
| 42 |
+
</svg>
|
| 43 |
+
"""
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
def display_avatar(self, state: str = 'neutral'):
|
| 47 |
+
"""Display the AI tutor avatar"""
|
| 48 |
+
st.markdown(f"""
|
| 49 |
+
<div style="display: flex; justify-content: center; margin: 20px 0;">
|
| 50 |
+
{self.avatar_states.get(state, self.avatar_states['neutral'])}
|
| 51 |
+
</div>
|
| 52 |
+
""", unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
def generate_response(self, user_input: str) -> str:
|
| 55 |
+
"""Generate contextualized response using Claude"""
|
| 56 |
+
context = get_tutor_context()
|
| 57 |
+
prompt = self.build_prompt(user_input, context)
|
| 58 |
+
|
| 59 |
+
# Generate response using Claude
|
| 60 |
+
message = self.client.messages.create(
|
| 61 |
+
model="claude-3-opus-20240229",
|
| 62 |
+
max_tokens=1024,
|
| 63 |
+
temperature=0.7,
|
| 64 |
+
system="You are an expert AI tutor, skilled at explaining complex concepts clearly and engaging students in their learning journey. Provide detailed, accurate explanations while maintaining an encouraging tone.",
|
| 65 |
+
messages=[
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": prompt
|
| 69 |
+
}
|
| 70 |
+
]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
response = message.content[0].text
|
| 74 |
+
|
| 75 |
+
# Analyze sentiment (Claude can do this as part of its response)
|
| 76 |
+
sentiment_score = 0.8 if any(word in response.lower() for word in ['great', 'excellent', 'good job', 'well done']) else 0.5
|
| 77 |
+
|
| 78 |
+
# Update engagement metrics
|
| 79 |
+
self.update_engagement_metrics(user_input, response, sentiment_score)
|
| 80 |
+
|
| 81 |
+
return response
|
| 82 |
+
|
| 83 |
+
def build_prompt(self, user_input: str, context: Dict) -> str:
|
| 84 |
+
"""Build a context-aware prompt for Claude"""
|
| 85 |
+
topic_context = f"Current topic: {context['current_topic']}\n" if context['current_topic'] else ""
|
| 86 |
+
|
| 87 |
+
chat_context = "\n".join([
|
| 88 |
+
f"Student: {msg['content']}" if msg['role'] == 'user' else f"Tutor: {msg['content']}"
|
| 89 |
+
for msg in context['chat_history'][-3:]
|
| 90 |
+
])
|
| 91 |
+
|
| 92 |
+
return f"""
|
| 93 |
+
You are an educational AI tutor helping a student learn.
|
| 94 |
+
|
| 95 |
+
{topic_context}
|
| 96 |
+
Difficulty level: {context['difficulty_level']}
|
| 97 |
+
Learning style: {context['learning_style']}
|
| 98 |
+
|
| 99 |
+
Previous conversation:
|
| 100 |
+
{chat_context}
|
| 101 |
+
|
| 102 |
+
Student: {user_input}
|
| 103 |
+
|
| 104 |
+
Provide a clear, helpful response that:
|
| 105 |
+
1. Addresses the student's question directly
|
| 106 |
+
2. Explains concepts in an engaging way
|
| 107 |
+
3. Uses examples when appropriate
|
| 108 |
+
4. Encourages deeper understanding
|
| 109 |
+
5. Maintains a supportive tone
|
| 110 |
+
|
| 111 |
+
If the question is about code, include working code examples.
|
| 112 |
+
If the topic is complex, break it down into simpler parts.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
def update_engagement_metrics(self, user_input: str, response: str, sentiment_score: float):
|
| 116 |
+
"""Update student engagement metrics"""
|
| 117 |
+
context = get_tutor_context()
|
| 118 |
+
context['engagement_metrics'].append({
|
| 119 |
+
'timestamp': datetime.now().isoformat(),
|
| 120 |
+
'sentiment_score': sentiment_score,
|
| 121 |
+
'interaction_length': len(user_input)
|
| 122 |
+
})
|