Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +64 -61
src/streamlit_app.py
CHANGED
@@ -3,14 +3,11 @@ from educhain import Educhain, LLMConfig
|
|
3 |
from educhain.engines import qna_engine
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
import os
|
6 |
-
from dotenv import load_dotenv
|
7 |
import json
|
8 |
from datetime import datetime
|
9 |
import pandas as pd
|
10 |
import random
|
11 |
|
12 |
-
# Load environment variables if available
|
13 |
-
load_dotenv()
|
14 |
|
15 |
# Set page configuration at the very top of the script
|
16 |
st.set_page_config(page_title="Multilingual Quiz App", page_icon="🧠", layout="wide")
|
@@ -56,7 +53,6 @@ if 'page' not in st.session_state:
|
|
56 |
|
57 |
# --- Sidebar Navigation ---
|
58 |
with st.sidebar:
|
59 |
-
st.sidebar.image("https://framerusercontent.com/images/T5kFJeyNUyAYJBz4PaWuP7Bfr0.png", use_container_width=True)
|
60 |
st.title("Multilingual Quiz App")
|
61 |
|
62 |
# Navigation
|
@@ -70,12 +66,10 @@ with st.sidebar:
|
|
70 |
|
71 |
st.header("⚙️ Configuration")
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
if not api_key:
|
78 |
-
st.warning("Please set your Sutra API Key as an environment variable or enter it above")
|
79 |
|
80 |
st.markdown("---")
|
81 |
st.markdown("**Powered by** [Educhain](https://github.com/satvik314/educhain)")
|
@@ -248,20 +242,16 @@ def show_create_quiz_page():
|
|
248 |
)
|
249 |
|
250 |
# --- Initialize Educhain client if API key is provided ---
|
251 |
-
api_key = os.getenv("SUTRA_API_KEY", "")
|
252 |
if not api_key:
|
253 |
-
|
254 |
-
|
255 |
-
if api_key:
|
256 |
-
educhain_client = initialize_educhain(api_key)
|
257 |
-
if educhain_client:
|
258 |
-
qna_engine = educhain_client.qna_engine
|
259 |
-
else:
|
260 |
-
st.error("Failed to initialize Educhain. Please check your Sutra API key.")
|
261 |
-
return
|
262 |
-
else:
|
263 |
-
st.warning("Please enter your Sutra API Key in the sidebar or set it as an environment variable to continue.")
|
264 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
# Quiz configuration
|
267 |
col1, col2, col3 = st.columns(3)
|
@@ -287,45 +277,58 @@ def show_create_quiz_page():
|
|
287 |
# Generate quiz button
|
288 |
if st.button("Generate Quiz"):
|
289 |
with st.spinner(f"Generating {num_questions} {selected_question_type.lower()} questions in {selected_language}..."):
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
|
330 |
# --- Saved Quizzes Page ---
|
331 |
def show_saved_quizzes_page():
|
|
|
3 |
from educhain.engines import qna_engine
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
import os
|
|
|
6 |
import json
|
7 |
from datetime import datetime
|
8 |
import pandas as pd
|
9 |
import random
|
10 |
|
|
|
|
|
11 |
|
12 |
# Set page configuration at the very top of the script
|
13 |
st.set_page_config(page_title="Multilingual Quiz App", page_icon="🧠", layout="wide")
|
|
|
53 |
|
54 |
# --- Sidebar Navigation ---
|
55 |
with st.sidebar:
|
|
|
56 |
st.title("Multilingual Quiz App")
|
57 |
|
58 |
# Navigation
|
|
|
66 |
|
67 |
st.header("⚙️ Configuration")
|
68 |
|
69 |
+
# API Key section
|
70 |
+
st.markdown("### API Key")
|
71 |
+
st.markdown("Get your free API key from [Sutra API](https://www.two.ai/sutra/api)")
|
72 |
+
api_key = st.text_input("Enter your Sutra API Key:", type="password")
|
|
|
|
|
73 |
|
74 |
st.markdown("---")
|
75 |
st.markdown("**Powered by** [Educhain](https://github.com/satvik314/educhain)")
|
|
|
242 |
)
|
243 |
|
244 |
# --- Initialize Educhain client if API key is provided ---
|
|
|
245 |
if not api_key:
|
246 |
+
st.warning("Please enter your Sutra API Key in the sidebar to continue.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
return
|
248 |
+
|
249 |
+
educhain_client = initialize_educhain(api_key)
|
250 |
+
if not educhain_client:
|
251 |
+
st.error("Failed to initialize Educhain. Please check your Sutra API key.")
|
252 |
+
return
|
253 |
+
|
254 |
+
qna_engine = educhain_client.qna_engine
|
255 |
|
256 |
# Quiz configuration
|
257 |
col1, col2, col3 = st.columns(3)
|
|
|
277 |
# Generate quiz button
|
278 |
if st.button("Generate Quiz"):
|
279 |
with st.spinner(f"Generating {num_questions} {selected_question_type.lower()} questions in {selected_language}..."):
|
280 |
+
try:
|
281 |
+
# Use the appropriate method based on question type
|
282 |
+
if selected_question_type == "Multiple Choice":
|
283 |
+
questions = qna_engine.generate_questions(
|
284 |
+
topic=topic,
|
285 |
+
num=num_questions,
|
286 |
+
question_type="Multiple Choice",
|
287 |
+
custom_instructions=language_custom_instructions,
|
288 |
+
difficulty=selected_difficulty.lower(),
|
289 |
+
language=selected_language
|
290 |
+
)
|
291 |
+
else: # True/False
|
292 |
+
questions = qna_engine.generate_questions(
|
293 |
+
topic=topic,
|
294 |
+
num=num_questions,
|
295 |
+
question_type="True/False",
|
296 |
+
custom_instructions=language_custom_instructions,
|
297 |
+
difficulty=selected_difficulty.lower(),
|
298 |
+
language=selected_language
|
299 |
+
)
|
300 |
+
|
301 |
+
if not questions or not hasattr(questions, "questions"):
|
302 |
+
st.error("Failed to generate questions. Please try again with different parameters.")
|
303 |
+
return
|
304 |
+
|
305 |
+
# Convert to quiz format and save
|
306 |
+
quiz = convert_to_quiz_format(questions, topic, selected_language, selected_difficulty)
|
307 |
+
quiz_id = save_quiz(quiz)
|
308 |
+
|
309 |
+
st.success(f"Quiz generated successfully! Quiz ID: {quiz_id}")
|
310 |
+
|
311 |
+
# Preview quiz
|
312 |
+
with st.expander("Preview Quiz"):
|
313 |
+
for i, q in enumerate(quiz["questions"]):
|
314 |
+
st.subheader(f"Question {i+1}: {q['question']}")
|
315 |
+
st.write("Options:")
|
316 |
+
for j, opt in enumerate(q["options"]):
|
317 |
+
st.write(f" {chr(65 + j)}. {opt}")
|
318 |
+
st.write(f"**Correct Answer:** {q['answer']}")
|
319 |
+
if "explanation" in q and q["explanation"]:
|
320 |
+
st.write(f"**Explanation:** {q['explanation']}")
|
321 |
+
st.markdown("---")
|
322 |
+
|
323 |
+
# Button to start quiz
|
324 |
+
if st.button("Start This Quiz"):
|
325 |
+
start_quiz(quiz)
|
326 |
+
st.session_state.page = "take"
|
327 |
+
st.rerun()
|
328 |
+
|
329 |
+
except Exception as e:
|
330 |
+
st.error(f"Error generating questions: {str(e)}")
|
331 |
+
st.error("Please try again with different parameters or check your API key.")
|
332 |
|
333 |
# --- Saved Quizzes Page ---
|
334 |
def show_saved_quizzes_page():
|