harshith1411 commited on
Commit
fdd04ac
·
verified ·
1 Parent(s): 60ba02f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_groq import ChatGroq
3
+ import json
4
+
5
+ # Application Title and Subheader
6
+ st.title('AI Quiz Generator')
7
+ st.subheader('Test Yourself!')
8
+
9
+ # Input Fields for Standard and Topic
10
+ A = st.text_input('Enter standard')
11
+ B = st.text_input('Enter a topic')
12
+
13
+ # Initialize the ChatGroq Model (replace with your actual API key)
14
+ model = ChatGroq(
15
+ temperature=0.6,
16
+ groq_api_key='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'
17
+
18
+ )
19
+
20
+ def generate_quiz(standard, topic, difficulty):
21
+ """
22
+ Generate a quiz with 5 multiple choice questions based on the difficulty level.
23
+ Returns structured JSON output.
24
+ """
25
+ prompt = (
26
+ f"Generate a quiz with exactly 5 multiple choice questions for a {standard} student on the topic of {topic}. "
27
+ "Each question should have four options and one correct answer. "
28
+ "Return the output in JSON format with the following structure: "
29
+ "{ \"questions\": [ {\"question\": \"...\", \"options\": [\"...\", \"...\", \"...\", \"...\"], \"answer\": \"...\"} ] }"
30
+ )
31
+ response = model.predict(prompt)
32
+ try:
33
+ return json.loads(response) # Safely parse JSON response
34
+ except json.JSONDecodeError:
35
+ st.error("Failed to parse quiz. Please try again.")
36
+ return None
37
+
38
+ def calculate_score(user_answers, correct_answers):
39
+ """Compare user answers with the correct answers and calculate the score."""
40
+ return sum(1 for q, ans in user_answers.items() if correct_answers.get(q) == ans)
41
+
42
+ # Session state to store quiz data
43
+ if 'quiz_data' not in st.session_state:
44
+ st.session_state.quiz_data = None
45
+ if 'user_answers' not in st.session_state:
46
+ st.session_state.user_answers = {}
47
+
48
+ # Difficulty Selection
49
+ difficulty_levels = ["Low", "Medium", "Hard"]
50
+ difficulty = st.radio("Select Difficulty Level", difficulty_levels)
51
+
52
+ # Generate Quiz Button
53
+ if st.button("Generate Quiz"):
54
+ if A and B:
55
+ st.session_state.quiz_data = generate_quiz(A, B, difficulty)
56
+ if st.session_state.quiz_data:
57
+ st.session_state.user_answers = {} # Reset user answers
58
+ st.success("Quiz Generated!")
59
+ else:
60
+ st.error("Please enter both the standard and topic.")
61
+
62
+ # Display Quiz if Available
63
+ if st.session_state.quiz_data:
64
+ st.write("### Quiz Questions:")
65
+ questions = st.session_state.quiz_data.get("questions", [])
66
+
67
+ for i, q in enumerate(questions[:5]): # Limit to 5 questions
68
+ st.write(f"**Q{i+1}: {q['question']}**")
69
+ user_answer = st.radio(
70
+ f"Select your answer for Question {i+1}",
71
+ options=q["options"],
72
+ key=f"question_{i+1}"
73
+ )
74
+ st.session_state.user_answers[f"question_{i+1}"] = user_answer
75
+
76
+ # Submit Quiz Button
77
+ if st.button("Submit Quiz"):
78
+ if st.session_state.quiz_data:
79
+ correct_answers = {f"question_{i+1}": q["answer"] for i, q in enumerate(st.session_state.quiz_data["questions"][:5])}
80
+ score = calculate_score(st.session_state.user_answers, correct_answers)
81
+ st.write("### Quiz Results")
82
+ st.write(f"Your answers: {st.session_state.user_answers}")
83
+ st.write(f"Correct answers: {correct_answers}")
84
+ st.write(f"Your score: {score}/5")
85
+ st.success("Great job! Keep practicing!")
86
+ else:
87
+ st.error("Quiz data not available. Please regenerate the quiz.")