Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,6 @@ import docx
|
|
| 7 |
from textwrap import wrap
|
| 8 |
import json
|
| 9 |
|
| 10 |
-
# Set page configuration (move this to the very start)
|
| 11 |
-
st.set_page_config(
|
| 12 |
-
page_title="EduAI Assistant",
|
| 13 |
-
page_icon="📚",
|
| 14 |
-
layout="wide"
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
# Initialize Groq client
|
| 18 |
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
| 19 |
|
|
@@ -89,7 +82,7 @@ def generate_mcqs(text, num_questions):
|
|
| 89 |
text (str): The input text for generating MCQs.
|
| 90 |
num_questions (int): The number of MCQs to generate.
|
| 91 |
Returns:
|
| 92 |
-
list: A list of MCQs
|
| 93 |
"""
|
| 94 |
messages = [
|
| 95 |
{"role": "system", "content": "Generate multiple-choice questions for a lesson."},
|
|
@@ -109,7 +102,14 @@ def generate_mcqs(text, num_questions):
|
|
| 109 |
# Load the JSON response
|
| 110 |
mcqs = json.loads(cleaned_response) # Attempt to parse the cleaned response
|
| 111 |
if isinstance(mcqs, list):
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
st.error("The generated MCQs are not in the expected format.")
|
| 115 |
return []
|
|
@@ -120,20 +120,27 @@ def generate_mcqs(text, num_questions):
|
|
| 120 |
st.error(f"Unexpected error: {e}")
|
| 121 |
return []
|
| 122 |
|
|
|
|
| 123 |
def adapt_lesson_for_grade(text, grade):
|
| 124 |
"""
|
| 125 |
Adapt lesson content for a specific grade level.
|
| 126 |
"""
|
| 127 |
messages = [
|
| 128 |
{"role": "system", "content": "Adapt the lesson content for a specific grade."},
|
| 129 |
-
{"role": "user", "content": f"Context: {text}\n\nAdapt this lesson for {grade}."},
|
| 130 |
]
|
| 131 |
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 132 |
return response.choices[0].message.content
|
| 133 |
|
| 134 |
# Streamlit app layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
st.title("EduAI Assistant for Teachers")
|
| 136 |
-
st.markdown("""
|
| 137 |
Welcome to the AI-powered teaching assistant!
|
| 138 |
- Upload your lesson files or input text.
|
| 139 |
- Ask questions, summarize topics, or create quizzes and assignments tailored to different grades.
|
|
@@ -194,16 +201,16 @@ if manual_input or uploaded_files:
|
|
| 194 |
st.write("### Multiple Choice Questions")
|
| 195 |
for i, mcq in enumerate(mcqs, 1):
|
| 196 |
st.write(f"**Q{i}. {mcq['question']}**")
|
| 197 |
-
for
|
| 198 |
-
st.write(f"
|
| 199 |
st.write(f"**Answer:** {mcq['answer']}")
|
| 200 |
st.write("---")
|
| 201 |
|
| 202 |
elif task == "Adapt Lesson for Grades":
|
| 203 |
-
grade = st.selectbox("Select the target grade level:",
|
| 204 |
if st.button("Adapt Lesson"):
|
| 205 |
adapted_lessons = [adapt_lesson_for_grade(chunk, grade) for chunk in text_chunks]
|
| 206 |
-
st.write(f"### Lesson Adapted for {grade}")
|
| 207 |
st.write("\n\n".join(adapted_lessons))
|
| 208 |
|
| 209 |
else:
|
|
|
|
| 7 |
from textwrap import wrap
|
| 8 |
import json
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Initialize Groq client
|
| 11 |
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
| 12 |
|
|
|
|
| 82 |
text (str): The input text for generating MCQs.
|
| 83 |
num_questions (int): The number of MCQs to generate.
|
| 84 |
Returns:
|
| 85 |
+
list: A list of MCQs, or an empty list if parsing fails.
|
| 86 |
"""
|
| 87 |
messages = [
|
| 88 |
{"role": "system", "content": "Generate multiple-choice questions for a lesson."},
|
|
|
|
| 102 |
# Load the JSON response
|
| 103 |
mcqs = json.loads(cleaned_response) # Attempt to parse the cleaned response
|
| 104 |
if isinstance(mcqs, list):
|
| 105 |
+
# Reformat the MCQs into the desired format
|
| 106 |
+
formatted_mcqs = []
|
| 107 |
+
for i, mcq in enumerate(mcqs, 1):
|
| 108 |
+
question = mcq['question']
|
| 109 |
+
options = "\n".join([f"{chr(97 + idx)}) {option}" for idx, option in enumerate(mcq['options'])])
|
| 110 |
+
answer = mcq['answer']
|
| 111 |
+
formatted_mcqs.append(f"**Q{i}. {question}**\n{options}\n\nAnswer: {answer}\n")
|
| 112 |
+
return formatted_mcqs
|
| 113 |
else:
|
| 114 |
st.error("The generated MCQs are not in the expected format.")
|
| 115 |
return []
|
|
|
|
| 120 |
st.error(f"Unexpected error: {e}")
|
| 121 |
return []
|
| 122 |
|
| 123 |
+
|
| 124 |
def adapt_lesson_for_grade(text, grade):
|
| 125 |
"""
|
| 126 |
Adapt lesson content for a specific grade level.
|
| 127 |
"""
|
| 128 |
messages = [
|
| 129 |
{"role": "system", "content": "Adapt the lesson content for a specific grade."},
|
| 130 |
+
{"role": "user", "content": f"Context: {text}\n\nAdapt this lesson for grade {grade}."},
|
| 131 |
]
|
| 132 |
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 133 |
return response.choices[0].message.content
|
| 134 |
|
| 135 |
# Streamlit app layout
|
| 136 |
+
st.set_page_config(
|
| 137 |
+
page_title="EduAI Assistant",
|
| 138 |
+
page_icon="📚",
|
| 139 |
+
layout="wide"
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
st.title("EduAI Assistant for Teachers")
|
| 143 |
+
st.markdown("""
|
| 144 |
Welcome to the AI-powered teaching assistant!
|
| 145 |
- Upload your lesson files or input text.
|
| 146 |
- Ask questions, summarize topics, or create quizzes and assignments tailored to different grades.
|
|
|
|
| 201 |
st.write("### Multiple Choice Questions")
|
| 202 |
for i, mcq in enumerate(mcqs, 1):
|
| 203 |
st.write(f"**Q{i}. {mcq['question']}**")
|
| 204 |
+
for option in mcq['options']:
|
| 205 |
+
st.write(f"- {option}")
|
| 206 |
st.write(f"**Answer:** {mcq['answer']}")
|
| 207 |
st.write("---")
|
| 208 |
|
| 209 |
elif task == "Adapt Lesson for Grades":
|
| 210 |
+
grade = st.selectbox("Select the target grade level:", list(range(1, 17))) # Grades 1 to 16
|
| 211 |
if st.button("Adapt Lesson"):
|
| 212 |
adapted_lessons = [adapt_lesson_for_grade(chunk, grade) for chunk in text_chunks]
|
| 213 |
+
st.write(f"### Lesson Adapted for Grade {grade}")
|
| 214 |
st.write("\n\n".join(adapted_lessons))
|
| 215 |
|
| 216 |
else:
|