Spaces:
Build error
Build error
File size: 3,218 Bytes
36bf65e 8564a3d c1d7673 36bf65e c1d7673 36bf65e b975cc5 36bf65e c1d7673 36bf65e d07a08e 36bf65e c1d7673 36bf65e c1d7673 36bf65e c1d7673 36bf65e c1d7673 | 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 | import streamlit as st
from langchain_groq import ChatGroq
import os
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
import tempfile
# Load API key
def getting_api_key():
return os.getenv("GROQ_KEY")
api_key = getting_api_key()
# Initialize LLM
llm = ChatGroq(
api_key=api_key,
model="llama-3.1-8b-instant"
)
st.title("π Study Question Generator")
# Input notes
context = st.text_area("Enter your notes:")
# Options
question_types = st.multiselect(
"Select question types:",
["Multiple Choice", "Short Answer", "True/False", "Essay / Analytical"],
default=["Short Answer"]
)
difficulty_level = st.selectbox(
"Select difficulty level:",
[
"Beginner (basic understanding)",
"Intermediate (application & explanation)",
"Advanced (critical thinking & between-the-lines)"
]
)
num_questions = st.slider("Number of questions:", 3, 20, 5)
# Session state
if "questions" not in st.session_state:
st.session_state.questions = ""
if "answers" not in st.session_state:
st.session_state.answers = ""
# Generate Questions
if st.button("Generate Questions"):
if context.strip() and question_types:
prompt = f"""
Generate {num_questions} study questions from the material below.
Material:
\"\"\"{context}\"\"\"
Question types: {", ".join(question_types)}
Difficulty: {difficulty_level}
Do NOT include answers.
"""
response = llm.invoke(prompt)
st.session_state.questions = response.content
st.session_state.answers = ""
# Show Questions
if st.session_state.questions:
st.subheader("π Questions")
st.write(st.session_state.questions)
# Generate Answers
if st.button("β
Generate Answers & Explanations"):
answer_prompt = f"""
You are an expert teacher.
Below are study questions. Provide:
- The correct answer
- A brief explanation for each question
Questions:
\"\"\"{st.session_state.questions}\"\"\"
Format clearly as:
Question:
Answer:
Explanation:
"""
answer_response = llm.invoke(answer_prompt)
st.session_state.answers = answer_response.content
# Show Answers
if st.session_state.answers:
st.subheader("π Answers & Explanations")
st.write(st.session_state.answers)
# PDF Export
if st.button("π Download Questions + Answers as PDF"):
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
c = canvas.Canvas(tmp.name, pagesize=A4)
width, height = A4
text = c.beginText(40, height - 50)
text.setFont("Helvetica", 11)
content = (
"STUDY QUESTIONS\n\n"
+ st.session_state.questions
+ "\n\nANSWERS & EXPLANATIONS\n\n"
+ st.session_state.answers
)
for line in content.split("\n"):
text.textLine(line)
c.drawText(text)
c.save()
with open(tmp.name, "rb") as f:
st.download_button(
"β¬οΈ Download PDF",
f,
file_name="study_questions_with_answers.pdf",
mime="application/pdf"
)
|