-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
190 lines (165 loc) · 7.58 KB
/
Copy pathapp.py
File metadata and controls
190 lines (165 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import streamlit as st
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process, LLM
# Load environment configuration
load_dotenv()
# Set up Streamlit Page Page Title & Premium design theme
st.set_page_config(
page_title="AI Study Buddy",
page_icon="🎓",
layout="centered"
)
# Custom premium CSS for visuals
st.markdown("""
<style>
.main {
background-color: #0e1117;
color: #ffffff;
}
.stButton>button {
background: linear-gradient(45deg, #FF4B4B, #FF8F8F);
color: white;
border: none;
padding: 10px 24px;
border-radius: 8px;
font-weight: bold;
transition: transform 0.2s;
}
.stButton>button:hover {
transform: scale(1.02);
color: white;
}
.explanation-box {
background-color: #262930;
color: #ffffff;
padding: 20px;
border-radius: 12px;
border-left: 5px solid #FF4B4B;
margin-bottom: 20px;
font-size: 1.05rem;
}
</style>
""", unsafe_allow_html=True)
# Initialize connection to local Ollama LLM
@st.cache_resource
def get_llm():
return LLM(
model=os.getenv("OPENAI_MODEL_NAME"),
base_url=os.getenv("OPENAI_API_BASE")
)
local_llm = get_llm()
# Initialize Agents
@st.cache_resource
def get_agents():
topic_agent = Agent(
role="Topic Explanation Expert",
goal="Provide clear, concise, and engaging explanations of educational topics.",
backstory="You are an expert educator who specializes in breaking down complex concepts "
"into simple, easy-to-understand explanations for students.",
llm=local_llm,
allow_delegation=False,
max_iter=3
)
quiz_agent = Agent(
role="Educational Quiz Designer",
goal="Design effective multiple-choice questions based on provided text.",
backstory="You are a professional assessment creator. You analyze educational materials "
"and construct clear, balanced multiple-choice questions to test comprehension.",
llm=local_llm,
allow_delegation=False,
max_iter=3
)
feedback_agent = Agent(
role="Tutor Evaluator",
goal="Evaluate the user's quiz answers against the topic material and provide constructive, friendly feedback.",
backstory="You are an encouraging and supportive private tutor. You review a student's answers, "
"explain why answers are correct or incorrect based on the educational material, and help them improve.",
llm=local_llm,
allow_delegation=False,
max_iter=3
)
return topic_agent, quiz_agent, feedback_agent
topic_agent, quiz_agent, feedback_agent = get_agents()
# Initialize session state variables to store outputs between steps
if "explanation" not in st.session_state:
st.session_state.explanation = ""
if "quiz" not in st.session_state:
st.session_state.quiz = ""
if "feedback" not in st.session_state:
st.session_state.feedback = ""
st.title("🎓 AI Study Buddy")
st.write("Your local sequential multi-agent learning companion powered by local Ollama.")
# Form to enter the study topic
topic_input = st.text_input("What topic would you like to study today?", placeholder="e.g., Photosynthesis, Gravity, AI...")
if st.button("Start Lesson"):
if topic_input:
with st.spinner("Topic Agent & Quiz Agent are preparing your lesson..."):
# Setup Tasks
explain_task = Task(
description=f"Generate a clear and concise explanation of the topic: '{topic_input}'. "
"Break down any complex terms and make it engaging for a beginner student.",
expected_output="A structured explanation of the topic with key definitions and a simple summary.",
agent=topic_agent
)
quiz_task = Task(
description="Analyze the generated explanation of the topic. Create exactly 3 multiple-choice questions (MCQs) "
"based on the material. Each question should have options (A, B, C, D). "
"CRITICAL: Do NOT show, mention, or print the correct answers anywhere in the generated quiz questions. "
"Keep them completely hidden so the student can answer them.",
expected_output="A list of 3 multiple-choice questions with choices, without showing the correct answers.",
agent=quiz_agent
)
# Sequential execution of explanation & quiz creation
crew_phase1 = Crew(
agents=[topic_agent, quiz_agent],
tasks=[explain_task, quiz_task],
process=Process.sequential
)
phase1_output = crew_phase1.kickoff()
# Store output in session state
st.session_state.explanation = explain_task.output.raw if explain_task.output else ""
st.session_state.quiz = quiz_task.output.raw if quiz_task.output else str(phase1_output)
st.session_state.feedback = "" # reset feedback for new topic
else:
st.warning("Please enter a topic first!")
# Display Phase 1 results if they exist
if st.session_state.explanation:
st.header("📖 1. Study material")
st.markdown(f'<div class="explanation-box">{st.session_state.explanation}</div>', unsafe_allow_html=True)
st.header("📝 2. Practice Quiz")
st.write("Review the generated questions below and write down your answers:")
st.text(st.session_state.quiz)
st.subheader("Submit your Answers")
ans1 = st.text_input("Answer for Question 1 (A/B/C/D):", key="ans1_val")
ans2 = st.text_input("Answer for Question 2 (A/B/C/D):", key="ans2_val")
ans3 = st.text_input("Answer for Question 3 (A/B/C/D):", key="ans3_val")
if st.button("Submit Answers"):
user_answers = f"Q1: {ans1}\nQ2: {ans2}\nQ3: {ans3}"
with st.spinner("Feedback Agent is evaluating your answers..."):
# Feedback Task setup
feedback_task = Task(
description=(
f"Review the original explanation:\n{st.session_state.explanation}\n\n"
f"Review the quiz questions:\n{st.session_state.quiz}\n\n"
f"Grade the student's answers:\n{user_answers}\n\n"
"Assess whether the answers are correct or incorrect. Provide friendly, clear explanation "
"for each question, explaining why the answer is correct or incorrect based on the material. "
"At the very beginning of your response, write a clear final score (e.g. 'Score: 2/3' or 'Score: 3/3'). "
"At the end of your message, sign off simply with 'Best regards,\nYour AI Study Buddy'. "
"Do NOT use brackets or placeholders like '[Your Name]' or '[Your Title]'."
),
expected_output="A friendly report starting with a final numeric score (e.g. Score: 2/3), grading each question, and signing off cleanly.",
agent=feedback_agent
)
crew_phase2 = Crew(
agents=[feedback_agent],
tasks=[feedback_task],
process=Process.sequential
)
final_feedback = crew_phase2.kickoff()
st.session_state.feedback = final_feedback.raw if hasattr(final_feedback, 'raw') else str(final_feedback)
# Display Tutor Feedback if it exists
if st.session_state.feedback:
st.header("🧑🏫 3. Tutor Feedback")
st.markdown(st.session_state.feedback)