Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 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 |
+
# Function to extract text from PDF
|
| 12 |
+
def extract_text_from_pdf(pdf_file):
|
| 13 |
+
pdf_reader = PdfReader(pdf_file)
|
| 14 |
+
text = ""
|
| 15 |
+
for page in pdf_reader.pages:
|
| 16 |
+
text += page.extract_text()
|
| 17 |
+
return text
|
| 18 |
+
|
| 19 |
+
# Function to extract text from Word document
|
| 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 |
+
# Helper Functions
|
| 26 |
+
|
| 27 |
+
def summarize_topic(text, topic):
|
| 28 |
+
messages = [
|
| 29 |
+
{"role": "system", "content": "Summarize the following lesson content."},
|
| 30 |
+
{"role": "user", "content": f"Context: {text}\n\nSummarize the topic: {topic}"},
|
| 31 |
+
]
|
| 32 |
+
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 33 |
+
return response.choices[0].message.content
|
| 34 |
+
|
| 35 |
+
def ask_question(text, question):
|
| 36 |
+
messages = [
|
| 37 |
+
{"role": "system", "content": "You are a helpful teaching assistant."},
|
| 38 |
+
{"role": "user", "content": f"Context: {text}\n\nQuestion: {question}"},
|
| 39 |
+
]
|
| 40 |
+
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 41 |
+
return response.choices[0].message.content
|
| 42 |
+
|
| 43 |
+
def generate_mcqs(text, num_questions):
|
| 44 |
+
messages = [
|
| 45 |
+
{"role": "system", "content": "Generate multiple-choice questions for a lesson."},
|
| 46 |
+
{"role": "user", "content": f"Context: {text}\n\nGenerate {num_questions} MCQs."},
|
| 47 |
+
]
|
| 48 |
+
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 49 |
+
return eval(response.choices[0].message.content)
|
| 50 |
+
|
| 51 |
+
def adapt_lesson_for_grade(text, grade):
|
| 52 |
+
messages = [
|
| 53 |
+
{"role": "system", "content": "Adapt the lesson content for a specific grade."},
|
| 54 |
+
{"role": "user", "content": f"Context: {text}\n\nAdapt this lesson for {grade}."},
|
| 55 |
+
]
|
| 56 |
+
response = client.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile", stream=False)
|
| 57 |
+
return response.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
# Streamlit app layout
|
| 60 |
+
st.title("AI Assistant for Teachers")
|
| 61 |
+
st.markdown("""
|
| 62 |
+
Welcome to the AI-powered teaching assistant!
|
| 63 |
+
- Upload your lesson files or input text.
|
| 64 |
+
- Ask questions, summarize topics, or create quizzes and assignments tailored to different grades.
|
| 65 |
+
""")
|
| 66 |
+
|
| 67 |
+
# Sidebar: File Upload and Options
|
| 68 |
+
st.sidebar.header("Upload Files or Enter Text")
|
| 69 |
+
uploaded_files = st.sidebar.file_uploader("Upload lesson files (PDFs or Word documents)", accept_multiple_files=True)
|
| 70 |
+
manual_input = st.sidebar.text_area("Or paste lesson text here", height=200)
|
| 71 |
+
|
| 72 |
+
# Select Functionality
|
| 73 |
+
st.sidebar.header("Select Action")
|
| 74 |
+
task = st.sidebar.selectbox("What would you like to do?", [
|
| 75 |
+
"Summarize a Topic",
|
| 76 |
+
"Ask Questions",
|
| 77 |
+
"Generate MCQs",
|
| 78 |
+
"Adapt Lesson for Grades"
|
| 79 |
+
])
|
| 80 |
+
|
| 81 |
+
# Main Actions
|
| 82 |
+
if manual_input or uploaded_files:
|
| 83 |
+
# Combine uploaded files into a single text
|
| 84 |
+
combined_text = ""
|
| 85 |
+
if uploaded_files:
|
| 86 |
+
for file in uploaded_files:
|
| 87 |
+
file_type = file.name.split(".")[-1].lower()
|
| 88 |
+
if file_type == "pdf":
|
| 89 |
+
combined_text += extract_text_from_pdf(file)
|
| 90 |
+
elif file_type == "docx":
|
| 91 |
+
combined_text += extract_text_from_word(file)
|
| 92 |
+
else:
|
| 93 |
+
st.error(f"Unsupported file type: {file_type}")
|
| 94 |
+
st.stop()
|
| 95 |
+
|
| 96 |
+
lesson_text = combined_text if uploaded_files else manual_input
|
| 97 |
+
|
| 98 |
+
if task == "Summarize a Topic":
|
| 99 |
+
topic = st.text_input("Enter the topic or keywords:")
|
| 100 |
+
if st.button("Summarize"):
|
| 101 |
+
summary = summarize_topic(lesson_text, topic)
|
| 102 |
+
st.write("### Summary")
|
| 103 |
+
st.write(summary)
|
| 104 |
+
|
| 105 |
+
elif task == "Ask Questions":
|
| 106 |
+
question = st.text_input("Enter your question:")
|
| 107 |
+
if st.button("Get Answer"):
|
| 108 |
+
answer = ask_question(lesson_text, question)
|
| 109 |
+
st.write("### Answer")
|
| 110 |
+
st.write(answer)
|
| 111 |
+
|
| 112 |
+
elif task == "Generate MCQs":
|
| 113 |
+
num_questions = st.slider("Number of questions to generate:", 1, 10, 5)
|
| 114 |
+
if st.button("Generate MCQs"):
|
| 115 |
+
mcqs = generate_mcqs(lesson_text, num_questions)
|
| 116 |
+
st.write("### Multiple Choice Questions")
|
| 117 |
+
for i, mcq in enumerate(mcqs, 1):
|
| 118 |
+
st.write(f"**Q{i}. {mcq['question']}**")
|
| 119 |
+
for option in mcq['options']:
|
| 120 |
+
st.write(f"- {option}")
|
| 121 |
+
st.write(f"**Answer:** {mcq['answer']}")
|
| 122 |
+
st.write("---")
|
| 123 |
+
|
| 124 |
+
elif task == "Adapt Lesson for Grades":
|
| 125 |
+
grade = st.selectbox("Select the target grade level:", ["Grade 1", "Grade 5", "Grade 8", "Grade 12"])
|
| 126 |
+
if st.button("Adapt Lesson"):
|
| 127 |
+
adapted_lesson = adapt_lesson_for_grade(lesson_text, grade)
|
| 128 |
+
st.write(f"### Lesson Adapted for {grade}")
|
| 129 |
+
st.write(adapted_lesson)
|
| 130 |
+
|
| 131 |
+
else:
|
| 132 |
+
st.info("Please upload files or enter lesson text to begin.")
|