File size: 2,811 Bytes
213f539
 
 
a417e3e
 
213f539
a417e3e
30cf447
 
a417e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511dc51
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
import streamlit as st
import openai

# Set your OpenAI API key from Hugging Face Secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]

# Initialize OpenAI client
client = openai.OpenAI(api_key=openai.api_key)

# Function to generate exam questions using OpenAI API
def generate_questions(knowledge_material, question_type, cognitive_level):
    prompt = f"Generate {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}"
    response = client.chat.completions.create(
        model="gpt-4o-mini",  # You can use "gpt-4" if available
        messages=[
            {"role": "system", "content": "You are a helpful assistant for generating exam questions."},
            {"role": "user", "content": prompt}
        ]
    )
    return response.choices[0].message.content

# Login page
if 'username' not in st.session_state:
    # Show the login form if the username is not set
    st.title("Login")
    username_input = st.text_input("Enter your username:")
    if st.button("Login"):
        if username_input:
            st.session_state['username'] = username_input
            st.success(f"Welcome, {username_input}!")
        else:
            st.warning("Please enter a valid username.")
else:
    # Main App after login
    st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions")

    # Input field for knowledge material (text)
    knowledge_material = st.text_area("Enter knowledge material to generate exam questions:")

    # File uploader for PDFs (Optional: Only if you want to implement PDF input later)
    uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")

    # Select question type
    question_type = st.selectbox("Select question type:", 
                                 ["Multiple Choice", "Fill in the Blank", "Open-ended"])

    # Select cognitive level
    cognitive_level = st.selectbox("Select cognitive level:", 
                                   ["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])

    # Generate questions button
    if st.button("Generate Questions"):
        if knowledge_material:
            # Generate questions using the OpenAI API
            questions = generate_questions(knowledge_material, question_type, cognitive_level)

            # Display the generated questions
            st.write("Generated Exam Questions:")
            st.write(questions)

            # Option to download the questions as a text file
            st.download_button(
                label="Download Questions",
                data=questions,
                file_name='generated_questions.txt',
                mime='text/plain'
            )
        else:
            st.warning("Please enter the knowledge material.")