Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Set up Streamlit page
|
6 |
+
st.title("Multiple Choice Quiz Generator")
|
7 |
+
st.markdown("Powered by Gemini API")
|
8 |
+
|
9 |
+
# **API Key Input from User**
|
10 |
+
gemini_api_key = st.text_input("Enter your Gemini API Key:", type="password")
|
11 |
+
|
12 |
+
if not gemini_api_key:
|
13 |
+
st.warning("Please enter your Gemini API key to generate a quiz.")
|
14 |
+
st.stop() # Stop execution if API key is missing
|
15 |
+
else:
|
16 |
+
genai.configure(api_key=gemini_api_key)
|
17 |
+
model = genai.GenerativeModel('gemini-pro') # Or 'gemini-pro-vision' if you need image input later
|
18 |
+
|
19 |
+
# **Security Warning**
|
20 |
+
st.markdown("<font color='red'>**Warning:** Directly entering your API key in the app is less secure than using Streamlit Secrets or environment variables, especially if this app is shared or deployed publicly. For production or sensitive use, consider using Streamlit Secrets instead.</font>", unsafe_allow_html=True)
|
21 |
+
|
22 |
+
|
23 |
+
# User input for topic
|
24 |
+
topic = st.text_input("Enter the topic for your quiz:")
|
25 |
+
|
26 |
+
if topic:
|
27 |
+
if st.button("Generate Quiz"):
|
28 |
+
with st.spinner(f"Generating quiz on '{topic}'..."):
|
29 |
+
try:
|
30 |
+
# Construct the prompt for Gemini
|
31 |
+
prompt = f"""
|
32 |
+
Generate a multiple-choice quiz on the topic of "{topic}".
|
33 |
+
The quiz should have 5 questions.
|
34 |
+
For each question, provide four plausible options labeled A, B, C, and D.
|
35 |
+
Clearly indicate the correct answer for each question at the end in a separate section called "Answer Key".
|
36 |
+
Format the quiz clearly for easy reading.
|
37 |
+
"""
|
38 |
+
|
39 |
+
response = model.generate_content(prompt)
|
40 |
+
quiz_content = response.text
|
41 |
+
|
42 |
+
if quiz_content:
|
43 |
+
st.markdown("### Quiz:")
|
44 |
+
st.markdown(quiz_content)
|
45 |
+
|
46 |
+
# Basic parsing to try and separate quiz and answer key (very basic, might need improvement)
|
47 |
+
if "Answer Key" in quiz_content:
|
48 |
+
parts = quiz_content.split("Answer Key")
|
49 |
+
quiz_questions = parts[0]
|
50 |
+
answer_key = "Answer Key" + parts[1] # Re-add "Answer Key" to the answer section
|
51 |
+
st.markdown("### Answer Key:")
|
52 |
+
st.markdown(answer_key)
|
53 |
+
else:
|
54 |
+
st.warning("Could not clearly separate answer key. Answers may be embedded in the quiz text.")
|
55 |
+
|
56 |
+
else:
|
57 |
+
st.error("Failed to generate quiz content. Please try again or check your API key.")
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
st.error(f"An error occurred: {e}")
|
61 |
+
st.error("Please check your API key and network connection. If the problem persists, try a different topic or try again later.")
|
62 |
+
else:
|
63 |
+
st.info("Please enter a topic to generate a quiz.")
|