Spaces:
Build error
Build error
File size: 9,461 Bytes
4cbe72a 84e95a0 dd59336 4cbe72a 2fa8b5b b51f9cc 4cbe72a 2fa8b5b 4cbe72a 2fa8b5b 4006790 2fa8b5b 4006790 9337c7d 4006790 1c73bea dd59336 4006790 2fa8b5b 4cbe72a 2fa8b5b 4cbe72a 2fa8b5b 4cbe72a 84e95a0 2fa8b5b 5e95f0c 2fa8b5b 5e95f0c 2fa8b5b 5e95f0c 2fa8b5b 4012753 6887739 4f6e688 6887739 4f6e688 2fa8b5b 84458d4 dd59336 d9e829f dd59336 4cbe72a 4006790 2fa8b5b 84e95a0 2fa8b5b 84e95a0 2fa8b5b 84e95a0 2fa8b5b 84e95a0 5e95f0c 84e95a0 dd59336 5e95f0c dec0180 88d5467 dec0180 88d5467 d9e829f dd59336 dec0180 dd59336 dbbee91 dd59336 7034cdf dd59336 5fedc33 2fa8b5b | 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import os
import streamlit as st
from groq import Groq
from io import BytesIO
from PyPDF2 import PdfReader
import docx
from textwrap import wrap
import json
# Configure Streamlit
st.set_page_config(
page_title="EduAI Assistant",
page_icon="📚",
layout="wide"
)
# Initialize Groq client
groq_api_key = os.environ.get("Groq_Api_Key")
if not groq_api_key:
st.error("Missing Groq API key! Please set the 'Groq_Api_Key' environment variable.")
st.stop()
client = Groq(api_key=groq_api_key)
# Sidebar Configuration
st.sidebar.header("Upload Files or Enter Text")
uploaded_files = st.sidebar.file_uploader(
"Upload lesson files (PDFs or Word documents)",
accept_multiple_files=True
)
manual_input = st.sidebar.text_area("Or paste lesson text here", height=200)
st.sidebar.header("Select Action")
task = st.sidebar.selectbox("What would you like to do?", [
"Summarize a Topic",
"Ask Questions",
"Generate MCQs",
"Adapt Lesson for Grades",
"Generate Conceptual Assignment",
"Provide Learning Resources",
"Generate Conceptual Short Questions"
])
# Helper functions
def extract_text_from_pdf(pdf_file):
pdf_reader = PdfReader(pdf_file)
return "".join(page.extract_text() for page in pdf_reader.pages)
def extract_text_from_word(doc_file):
doc = docx.Document(doc_file)
return "\n".join(paragraph.text for paragraph in doc.paragraphs)
def chunk_text(text, max_tokens=2000):
char_limit = max_tokens * 4 # Approximation: 1 token = 4 characters
return wrap(text, char_limit)
def process_with_groq(messages, model="llama-3.3-70b-versatile"):
try:
response = client.chat.completions.create(messages=messages, model=model, stream=False)
return response.choices[0].message.content.strip()
except Exception as e:
st.error(f"Error with Groq API: {e}")
return ""
def save_to_docx(text, filename="download.docx"):
doc = docx.Document()
doc.add_paragraph(text)
byte_io = BytesIO()
doc.save(byte_io)
byte_io.seek(0)
return byte_io
# Main App Layout
st.title("EduAI-Assistant for Teachers")
st.markdown("""
Welcome to your AI-powered teaching assistant!
- Upload lesson files or input text.
- Perform actions like summarizing topics, generating assignments/quizzez/short questions, generating learning resources and adapting lessons.
""")
if uploaded_files or manual_input:
lesson_text = ""
if uploaded_files:
for file in uploaded_files:
file_type = file.name.split(".")[-1].lower()
if file_type == "pdf":
lesson_text += extract_text_from_pdf(file)
elif file_type == "docx":
lesson_text += extract_text_from_word(file)
else:
st.error(f"Unsupported file type: {file_type}")
st.stop()
else:
lesson_text = manual_input
text_chunks = chunk_text(lesson_text)
if task == "Summarize a Topic":
topic = st.text_input("Enter the topic or keywords:")
if st.button("Summarize"):
summaries = [process_with_groq([
{"role": "system", "content": "Summarize the following lesson content."},
{"role": "user", "content": f"Context: {chunk}\n\nSummarize the topic: {topic}"}
]) for chunk in text_chunks]
st.write("### Summary")
summary_text = "\n\n".join(summaries)
st.write(summary_text)
docx_file = save_to_docx(summary_text)
st.download_button("Download Summary as DOCX", docx_file, file_name="summary.docx")
elif task == "Ask Questions":
question = st.text_input("Enter your question:")
if st.button("Get Answer"):
answers = [process_with_groq([
{"role": "system", "content": "You are a helpful teaching assistant."},
{"role": "user", "content": f"Context: {chunk}\n\nQuestion: {question}"}
]) for chunk in text_chunks]
st.write("### Answer")
st.write("\n\n".join(answers))
elif task == "Generate MCQs":
num_questions = st.slider("Number of questions to generate:", 1, 10, 5)
if st.button("Generate MCQs"):
mcqs = []
for chunk in text_chunks:
response = process_with_groq([
{"role": "system", "content": "You are an AI assistant generating multiple-choice questions. Please provide each MCQ in a clearly structured format with the following fields: question, options (list of options), and answer. Separate each question with a newline."},
{"role": "user", "content": f"Context: {chunk}\n\nGenerate {num_questions} MCQs in a structured format."}
])
# Check if the response contains a valid structure (e.g., 'Q1:', 'Options:')
if "Q" in response and "Options:" in response:
# Split the response into individual MCQs using a delimiter like 'Q' or a newline
mcq_blocks = response.split("\n")
for block in mcq_blocks:
if block.strip().startswith("Q"):
question = block.strip()
options = []
answer = ""
# Extract options and answer
for option_line in mcq_blocks:
if option_line.startswith("Options:"):
options = option_line[len("Options:"):].split(" ")
if option_line.startswith("Answer:"):
answer = option_line[len("Answer:"):].strip()
mcqs.append({
"question": question,
"options": options,
"answer": answer
})
else:
st.error(f"Failed to parse structured MCQs from the response: {response}")
st.write("### Multiple Choice Questions")
for idx, mcq in enumerate(mcqs):
st.write(f"**Q{idx + 1}:** {mcq['question']}")
for option in mcq['options']:
st.write(f"- {option}")
# Create the MCQs text for download
mcqs_text = "\n\n".join([f"**Q{idx + 1}:** {mcq['question']}\n" + "\n".join([f"- {option}" for option in mcq['options']]) for idx, mcq in enumerate(mcqs)])
mcqs_file = save_to_docx(mcqs_text, filename="mcqs.docx")
st.download_button("Download MCQs as DOCX", mcqs_file, file_name="mcqs.docx")
elif task == "Adapt Lesson for Grades":
grade = st.slider("Select Grade:", 1, 16, 9)
if st.button("Adapt Lesson"):
adaptations = [process_with_groq([
{"role": "system", "content": "Adapt the lesson content for a specific grade."},
{"role": "user", "content": f"Context: {chunk}\n\nAdapt this lesson for grade {grade}."}
]) for chunk in text_chunks]
st.write("### Adapted Lesson")
st.write("\n\n".join(adaptations))
elif task == "Generate Conceptual Assignment":
topic = st.text_input("Enter the topic for the assignment:")
if st.button("Generate Assignment"):
assignments = [process_with_groq([
{"role": "system", "content": "Generate a conceptual-based assignment."},
{"role": "user", "content": f"Context: {chunk}\n\nTopic: {topic}"}
]) for chunk in text_chunks]
st.write("### Conceptual Assignment")
assignment_text = "\n\n".join(assignments)
st.write(assignment_text) # Display the assignment first
docx_file = save_to_docx(assignment_text)
st.download_button("Download Assignment as DOCX", docx_file, file_name="assignment.docx")
elif task == "Provide Learning Resources":
topic = st.text_input("Enter the topic:")
if st.button("Generate Resources"):
resources = process_with_groq([
{"role": "system", "content": "Provide a list of learning resources for a topic."},
{"role": "user", "content": f"Topic: {topic}"}
])
st.write("### Learning Resources")
st.write(resources)
elif task == "Generate Conceptual Short Questions":
topic = st.text_input("Enter the topic for conceptual short questions:")
if st.button("Generate Conceptual Short Questions"):
conceptual_questions = [process_with_groq([
{"role": "system", "content": "Generate conceptual short questions based on the provided lesson content."},
{"role": "user", "content": f"Context: {chunk}\n\nGenerate deep conceptual questions for the topic: {topic}"}
]) for chunk in text_chunks]
st.write("### Conceptual Short Questions")
conceptual_questions_text = "\n\n".join(conceptual_questions)
st.write(conceptual_questions_text)
docx_file = save_to_docx(conceptual_questions_text)
st.download_button("Download Conceptual Short Questions as DOCX", docx_file, file_name="conceptual_short_questions.docx")
else:
st.info("Please upload files or enter lesson text to get started.")
|