Rathapoom commited on
Commit
a417e3e
1 Parent(s): 511dc51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -25
app.py CHANGED
@@ -1,32 +1,70 @@
1
  import streamlit as st
2
  import openai
3
 
4
- # Set your OpenAI API key using Streamlit secrets
5
- openai.api_key = "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"
6
 
7
- # Create an OpenAI client using the new API structure
8
  client = openai.OpenAI(api_key=openai.api_key)
9
 
10
- # Streamlit app
11
- st.title("Exam Question Generator")
12
-
13
- # Input field for user prompt
14
- user_input = st.text_area("Enter knowledge material to generate exam questions:")
15
-
16
- if st.button("Generate Questions"):
17
- if user_input:
18
- # Call OpenAI's Chat Completion API using the new structure
19
- response = client.chat.completions.create(
20
- model="gpt-3.5-turbo", # Or use "gpt-4" if available
21
- messages=[
22
- {"role": "system", "content": "You are a helpful assistant for generating exam questions."},
23
- {"role": "user", "content": f"Generate exam questions from the following material: {user_input}"}
24
- ]
25
- )
26
-
27
- # Extract the response and display it correctly
28
- st.write("Generated Exam Questions:")
29
- st.write(response.choices[0].message.content) # Access message content directly
30
- else:
31
- st.warning("Please enter the knowledge material.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
1
  import streamlit as st
2
  import openai
3
 
4
+ # Set your OpenAI API key from Hugging Face Secrets
5
+ openai.api_key = st.secrets["OPENAI_API_KEY"]
6
 
7
+ # Initialize OpenAI client
8
  client = openai.OpenAI(api_key=openai.api_key)
9
 
10
+ # Function to generate exam questions using OpenAI API
11
+ def generate_questions(knowledge_material, question_type, cognitive_level):
12
+ prompt = f"Generate {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}"
13
+ response = client.chat.completions.create(
14
+ model="gpt-4o-mini", # You can use "gpt-4" if available
15
+ messages=[
16
+ {"role": "system", "content": "You are a helpful assistant for generating exam questions."},
17
+ {"role": "user", "content": prompt}
18
+ ]
19
+ )
20
+ return response.choices[0].message.content
21
+
22
+ # Login page
23
+ if 'username' not in st.session_state:
24
+ # Show the login form if the username is not set
25
+ st.title("Login")
26
+ username_input = st.text_input("Enter your username:")
27
+ if st.button("Login"):
28
+ if username_input:
29
+ st.session_state['username'] = username_input
30
+ st.success(f"Welcome, {username_input}!")
31
+ else:
32
+ st.warning("Please enter a valid username.")
33
+ else:
34
+ # Main App after login
35
+ st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions")
36
+
37
+ # Input field for knowledge material (text)
38
+ knowledge_material = st.text_area("Enter knowledge material to generate exam questions:")
39
+
40
+ # File uploader for PDFs (Optional: Only if you want to implement PDF input later)
41
+ uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")
42
+
43
+ # Select question type
44
+ question_type = st.selectbox("Select question type:",
45
+ ["Multiple Choice", "Fill in the Blank", "Open-ended"])
46
+
47
+ # Select cognitive level
48
+ cognitive_level = st.selectbox("Select cognitive level:",
49
+ ["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])
50
+
51
+ # Generate questions button
52
+ if st.button("Generate Questions"):
53
+ if knowledge_material:
54
+ # Generate questions using the OpenAI API
55
+ questions = generate_questions(knowledge_material, question_type, cognitive_level)
56
+
57
+ # Display the generated questions
58
+ st.write("Generated Exam Questions:")
59
+ st.write(questions)
60
+
61
+ # Option to download the questions as a text file
62
+ st.download_button(
63
+ label="Download Questions",
64
+ data=questions,
65
+ file_name='generated_questions.txt',
66
+ mime='text/plain'
67
+ )
68
+ else:
69
+ st.warning("Please enter the knowledge material.")
70