Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,12 @@ from groq import Groq
|
|
| 4 |
from io import BytesIO
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
import docx
|
| 7 |
-
#import json
|
| 8 |
|
| 9 |
# Initialize Groq client
|
| 10 |
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
| 11 |
|
| 12 |
-
#
|
|
|
|
| 13 |
def extract_text_from_pdf(pdf_file):
|
| 14 |
pdf_reader = PdfReader(pdf_file)
|
| 15 |
text = ""
|
|
@@ -17,14 +17,11 @@ def extract_text_from_pdf(pdf_file):
|
|
| 17 |
text += page.extract_text()
|
| 18 |
return text
|
| 19 |
|
| 20 |
-
# Function to extract text from Word document
|
| 21 |
def extract_text_from_word(doc_file):
|
| 22 |
doc = docx.Document(doc_file)
|
| 23 |
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
| 24 |
return text
|
| 25 |
|
| 26 |
-
# Helper Functions
|
| 27 |
-
|
| 28 |
def summarize_topic(text, topic):
|
| 29 |
messages = [
|
| 30 |
{"role": "system", "content": "Summarize the following lesson content."},
|
|
@@ -48,7 +45,6 @@ def generate_mcqs(text, num_questions):
|
|
| 48 |
]
|
| 49 |
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 50 |
return eval(response.choices[0].message.content)
|
| 51 |
-
|
| 52 |
|
| 53 |
def adapt_lesson_for_grade(text, grade):
|
| 54 |
messages = [
|
|
@@ -58,77 +54,130 @@ def adapt_lesson_for_grade(text, grade):
|
|
| 58 |
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 59 |
return response.choices[0].message.content
|
| 60 |
|
| 61 |
-
# Streamlit
|
| 62 |
-
st.
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Sidebar: File Upload and Options
|
| 70 |
-
st.sidebar
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
st.
|
| 76 |
-
task = st.
|
| 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 |
-
st.
|
| 119 |
-
|
| 120 |
-
st.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
st.write(
|
| 124 |
-
st.write(
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
import docx
|
|
|
|
| 7 |
|
| 8 |
# Initialize Groq client
|
| 9 |
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
| 10 |
|
| 11 |
+
# Helper Functions
|
| 12 |
+
|
| 13 |
def extract_text_from_pdf(pdf_file):
|
| 14 |
pdf_reader = PdfReader(pdf_file)
|
| 15 |
text = ""
|
|
|
|
| 17 |
text += page.extract_text()
|
| 18 |
return text
|
| 19 |
|
|
|
|
| 20 |
def extract_text_from_word(doc_file):
|
| 21 |
doc = docx.Document(doc_file)
|
| 22 |
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
| 23 |
return text
|
| 24 |
|
|
|
|
|
|
|
| 25 |
def summarize_topic(text, topic):
|
| 26 |
messages = [
|
| 27 |
{"role": "system", "content": "Summarize the following lesson content."},
|
|
|
|
| 45 |
]
|
| 46 |
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 47 |
return eval(response.choices[0].message.content)
|
|
|
|
| 48 |
|
| 49 |
def adapt_lesson_for_grade(text, grade):
|
| 50 |
messages = [
|
|
|
|
| 54 |
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 55 |
return response.choices[0].message.content
|
| 56 |
|
| 57 |
+
# Streamlit App Layout
|
| 58 |
+
st.set_page_config(
|
| 59 |
+
page_title="EduAI Assistant",
|
| 60 |
+
page_icon="\ud83d\udcda",
|
| 61 |
+
layout="wide",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Header Section with Branding
|
| 65 |
+
st.markdown(
|
| 66 |
+
"""
|
| 67 |
+
<style>
|
| 68 |
+
.main-header {
|
| 69 |
+
text-align: center;
|
| 70 |
+
background-color: #f4f4f4;
|
| 71 |
+
padding: 15px;
|
| 72 |
+
border-radius: 5px;
|
| 73 |
+
}
|
| 74 |
+
</style>
|
| 75 |
+
<div class="main-header">
|
| 76 |
+
<h1>EduAI - Your Teaching Assistant</h1>
|
| 77 |
+
<p>AI-powered tools to assist educators with lesson planning, quizzes, and content adaptation.</p>
|
| 78 |
+
</div>
|
| 79 |
+
""",
|
| 80 |
+
unsafe_allow_html=True,
|
| 81 |
+
)
|
| 82 |
|
| 83 |
# Sidebar: File Upload and Options
|
| 84 |
+
with st.sidebar:
|
| 85 |
+
st.header("\ud83c\udf93 Upload Lessons or Enter Text")
|
| 86 |
+
uploaded_files = st.file_uploader("Upload lesson files (PDFs or Word documents)", accept_multiple_files=True)
|
| 87 |
+
manual_input = st.text_area("Or paste lesson text here", height=200)
|
| 88 |
+
|
| 89 |
+
st.header("\u2699\ufe0f Choose Action")
|
| 90 |
+
task = st.selectbox("What would you like to do?", [
|
| 91 |
+
"Summarize a Topic",
|
| 92 |
+
"Ask Questions",
|
| 93 |
+
"Generate MCQs",
|
| 94 |
+
"Adapt Lesson for Grades"
|
| 95 |
+
])
|
| 96 |
+
|
| 97 |
+
st.header("\ud83d\udd04 Additional Options")
|
| 98 |
+
st.checkbox("Show raw outputs", key="show_raw_outputs")
|
| 99 |
+
|
| 100 |
+
# Main Layout with Tabs
|
| 101 |
+
tabs = st.tabs(["\ud83d\udcc2 Lesson Input", "\ud83d\udcdc Outputs", "\ud83d\udd8b\ufe0f Help & Info"])
|
| 102 |
+
|
| 103 |
+
# Lesson Input Tab
|
| 104 |
+
with tabs[0]:
|
| 105 |
+
st.subheader("Lesson Text Preview")
|
| 106 |
+
if manual_input or uploaded_files:
|
| 107 |
+
combined_text = ""
|
| 108 |
+
if uploaded_files:
|
| 109 |
+
for file in uploaded_files:
|
| 110 |
+
file_type = file.name.split(".")[-1].lower()
|
| 111 |
+
try:
|
| 112 |
+
if file_type == "pdf":
|
| 113 |
+
combined_text += extract_text_from_pdf(file)
|
| 114 |
+
elif file_type == "docx":
|
| 115 |
+
combined_text += extract_text_from_word(file)
|
| 116 |
+
else:
|
| 117 |
+
st.error(f"Unsupported file type: {file_type}")
|
| 118 |
+
st.stop()
|
| 119 |
+
except Exception as e:
|
| 120 |
+
st.error(f"Error reading file {file.name}: {e}")
|
| 121 |
+
|
| 122 |
+
lesson_text = combined_text if uploaded_files else manual_input
|
| 123 |
+
st.text_area("Lesson Content:", value=lesson_text, height=300)
|
| 124 |
+
else:
|
| 125 |
+
st.info("Please upload files or enter lesson text to begin.")
|
| 126 |
+
|
| 127 |
+
# Outputs Tab
|
| 128 |
+
with tabs[1]:
|
| 129 |
+
st.subheader("Results")
|
| 130 |
+
if manual_input or uploaded_files:
|
| 131 |
+
if task == "Summarize a Topic":
|
| 132 |
+
topic = st.text_input("Enter the topic or keywords:")
|
| 133 |
+
if st.button("Summarize"):
|
| 134 |
+
with st.spinner("Summarizing..."):
|
| 135 |
+
summary = summarize_topic(lesson_text, topic)
|
| 136 |
+
st.success("Summary generated!")
|
| 137 |
+
st.write("### Summary")
|
| 138 |
+
st.write(summary)
|
| 139 |
+
|
| 140 |
+
elif task == "Ask Questions":
|
| 141 |
+
question = st.text_input("Enter your question:")
|
| 142 |
+
if st.button("Get Answer"):
|
| 143 |
+
with st.spinner("Finding answer..."):
|
| 144 |
+
answer = ask_question(lesson_text, question)
|
| 145 |
+
st.success("Answer retrieved!")
|
| 146 |
+
st.write("### Answer")
|
| 147 |
+
st.write(answer)
|
| 148 |
+
|
| 149 |
+
elif task == "Generate MCQs":
|
| 150 |
+
num_questions = st.slider("Number of questions to generate:", 1, 10, 5)
|
| 151 |
+
if st.button("Generate MCQs"):
|
| 152 |
+
with st.spinner("Generating MCQs..."):
|
| 153 |
+
mcqs = generate_mcqs(lesson_text, num_questions)
|
| 154 |
+
st.success("MCQs generated!")
|
| 155 |
+
for i, mcq in enumerate(mcqs, 1):
|
| 156 |
+
st.write(f"**Q{i}. {mcq['question']}**")
|
| 157 |
+
for option in mcq['options']:
|
| 158 |
+
st.write(f"- {option}")
|
| 159 |
+
st.write(f"**Answer:** {mcq['answer']}")
|
| 160 |
+
st.write("---")
|
| 161 |
+
|
| 162 |
+
elif task == "Adapt Lesson for Grades":
|
| 163 |
+
grade = st.selectbox("Select the target grade level:", ["Grade 1", "Grade 5", "Grade 8", "Grade 12"])
|
| 164 |
+
if st.button("Adapt Lesson"):
|
| 165 |
+
with st.spinner("Adapting lesson..."):
|
| 166 |
+
adapted_lesson = adapt_lesson_for_grade(lesson_text, grade)
|
| 167 |
+
st.success(f"Lesson adapted for {grade}!")
|
| 168 |
+
st.write(f"### Lesson Adapted for {grade}")
|
| 169 |
+
st.write(adapted_lesson)
|
| 170 |
+
else:
|
| 171 |
+
st.info("No text or files uploaded. Please provide input.")
|
| 172 |
+
|
| 173 |
+
# Help & Info Tab
|
| 174 |
+
with tabs[2]:
|
| 175 |
+
st.subheader("How to Use EduAI")
|
| 176 |
+
st.markdown(
|
| 177 |
+
"""
|
| 178 |
+
- **Upload Files**: Drag and drop your PDF or Word documents.
|
| 179 |
+
- **Choose Action**: Select the task you want EduAI to perform.
|
| 180 |
+
- **View Outputs**: Check the results under the Outputs tab.
|
| 181 |
+
- **Contact Support**: For any issues, contact [support@eduai.com](mailto:support@eduai.com).
|
| 182 |
+
"""
|
| 183 |
+
)
|