Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
|
|
|
| 3 |
import os
|
| 4 |
import re # For improved parsing
|
| 5 |
|
|
@@ -7,24 +8,39 @@ import re # For improved parsing
|
|
| 7 |
def login_page():
|
| 8 |
st.title("Quiz Generator Login")
|
| 9 |
|
| 10 |
-
api_options = ["Gemini API"
|
| 11 |
selected_api = st.selectbox("Select API:", api_options)
|
| 12 |
api_key = st.text_input(f"Enter your {selected_api} Key:", type="password")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if st.button("Login"):
|
| 15 |
if not api_key:
|
| 16 |
st.error("Please enter your API key.")
|
| 17 |
else:
|
| 18 |
st.session_state.api_key = api_key
|
| 19 |
st.session_state.selected_api = selected_api
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
st.session_state.logged_in = True
|
| 21 |
-
st.success(f"Logged in with {selected_api}!")
|
| 22 |
st.rerun() # Rerun to show the quiz app
|
| 23 |
|
| 24 |
# --- Quiz Application ---
|
| 25 |
def quiz_app():
|
| 26 |
st.title("Interactive Multiple Choice Quiz Generator")
|
| 27 |
-
st.markdown("Powered by
|
| 28 |
|
| 29 |
# Security Warning - Moved here as it is part of the quiz app
|
| 30 |
st.markdown("<font color='red'>**Warning:** Directly entering your API key is less secure than using Streamlit Secrets or environment variables. For production, use Secrets.</font>", unsafe_allow_html=True)
|
|
@@ -45,7 +61,7 @@ def quiz_app():
|
|
| 45 |
if st.session_state.selected_api == "Gemini API":
|
| 46 |
try:
|
| 47 |
genai.configure(api_key=st.session_state.api_key)
|
| 48 |
-
model = genai.GenerativeModel(
|
| 49 |
except Exception as e:
|
| 50 |
st.error(f"Error configuring Gemini API: {e}")
|
| 51 |
st.error("Please check your API key and network connection.")
|
|
@@ -54,6 +70,18 @@ def quiz_app():
|
|
| 54 |
del st.session_state.selected_api
|
| 55 |
st.rerun() # Go back to login page
|
| 56 |
return # Exit quiz_app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
def parse_quiz_content(quiz_content):
|
|
@@ -225,8 +253,15 @@ def quiz_app():
|
|
| 225 |
|
| 226 |
Please generate the quiz in this exact format.
|
| 227 |
"""
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
|
| 232 |
parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
import openai
|
| 4 |
import os
|
| 5 |
import re # For improved parsing
|
| 6 |
|
|
|
|
| 8 |
def login_page():
|
| 9 |
st.title("Quiz Generator Login")
|
| 10 |
|
| 11 |
+
api_options = ["Gemini API", "OpenAI API"]
|
| 12 |
selected_api = st.selectbox("Select API:", api_options)
|
| 13 |
api_key = st.text_input(f"Enter your {selected_api} Key:", type="password")
|
| 14 |
|
| 15 |
+
if selected_api == "Gemini API":
|
| 16 |
+
model_options = ["gemini-pro"] # Add more Gemini models if available
|
| 17 |
+
selected_model = st.selectbox("Select Gemini Model:", model_options, index=0) # Default to first model
|
| 18 |
+
elif selected_api == "OpenAI API":
|
| 19 |
+
model_options = ["gpt-3.5-turbo", "gpt-4"] # Add more OpenAI models as needed
|
| 20 |
+
selected_model = st.selectbox("Select OpenAI Model:", model_options, index=0) # Default to first model
|
| 21 |
+
else:
|
| 22 |
+
selected_model = None # Should not happen, but for safety
|
| 23 |
+
|
| 24 |
if st.button("Login"):
|
| 25 |
if not api_key:
|
| 26 |
st.error("Please enter your API key.")
|
| 27 |
else:
|
| 28 |
st.session_state.api_key = api_key
|
| 29 |
st.session_state.selected_api = selected_api
|
| 30 |
+
if selected_api == "Gemini API":
|
| 31 |
+
st.session_state.gemini_model = selected_model
|
| 32 |
+
st.session_state.openai_model = None # Clear OpenAI model if Gemini is selected
|
| 33 |
+
elif selected_api == "OpenAI API":
|
| 34 |
+
st.session_state.openai_model = selected_model
|
| 35 |
+
st.session_state.gemini_model = None # Clear Gemini model if OpenAI is selected
|
| 36 |
st.session_state.logged_in = True
|
| 37 |
+
st.success(f"Logged in with {selected_api} using model: {selected_model}!")
|
| 38 |
st.rerun() # Rerun to show the quiz app
|
| 39 |
|
| 40 |
# --- Quiz Application ---
|
| 41 |
def quiz_app():
|
| 42 |
st.title("Interactive Multiple Choice Quiz Generator")
|
| 43 |
+
st.markdown(f"Powered by {st.session_state.selected_api}")
|
| 44 |
|
| 45 |
# Security Warning - Moved here as it is part of the quiz app
|
| 46 |
st.markdown("<font color='red'>**Warning:** Directly entering your API key is less secure than using Streamlit Secrets or environment variables. For production, use Secrets.</font>", unsafe_allow_html=True)
|
|
|
|
| 61 |
if st.session_state.selected_api == "Gemini API":
|
| 62 |
try:
|
| 63 |
genai.configure(api_key=st.session_state.api_key)
|
| 64 |
+
model = genai.GenerativeModel(st.session_state.gemini_model)
|
| 65 |
except Exception as e:
|
| 66 |
st.error(f"Error configuring Gemini API: {e}")
|
| 67 |
st.error("Please check your API key and network connection.")
|
|
|
|
| 70 |
del st.session_state.selected_api
|
| 71 |
st.rerun() # Go back to login page
|
| 72 |
return # Exit quiz_app
|
| 73 |
+
elif st.session_state.selected_api == "OpenAI API":
|
| 74 |
+
try:
|
| 75 |
+
openai.api_key = st.session_state.api_key
|
| 76 |
+
model_name = st.session_state.openai_model # Model name already selected in login
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"Error configuring OpenAI API: {e}")
|
| 79 |
+
st.error("Please check your API key and network connection.")
|
| 80 |
+
st.session_state.logged_in = False # Force logout if API config fails
|
| 81 |
+
del st.session_state.api_key
|
| 82 |
+
del st.session_state.selected_api
|
| 83 |
+
st.rerun() # Go back to login page
|
| 84 |
+
return # Exit quiz_app
|
| 85 |
|
| 86 |
|
| 87 |
def parse_quiz_content(quiz_content):
|
|
|
|
| 253 |
|
| 254 |
Please generate the quiz in this exact format.
|
| 255 |
"""
|
| 256 |
+
if st.session_state.selected_api == "Gemini API":
|
| 257 |
+
response = model.generate_content(prompt)
|
| 258 |
+
quiz_content = response.text
|
| 259 |
+
elif st.session_state.selected_api == "OpenAI API":
|
| 260 |
+
response = openai.ChatCompletion.create(
|
| 261 |
+
model=st.session_state.openai_model,
|
| 262 |
+
messages=[{"role": "user", "content": prompt}]
|
| 263 |
+
)
|
| 264 |
+
quiz_content = response.choices[0].message.content
|
| 265 |
|
| 266 |
|
| 267 |
parsed_quiz_data, answer_key = parse_quiz_content(quiz_content)
|