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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Retrieve the API key from environment variables
|
| 10 |
+
api_key = os.getenv('GROQ_API_KEY')
|
| 11 |
+
|
| 12 |
+
# Initialize ChatGroq
|
| 13 |
+
llm = ChatGroq(
|
| 14 |
+
api_key=api_key,
|
| 15 |
+
model="llama-3.1-70b-versatile", # Specify your desired model
|
| 16 |
+
temperature=1.0, # Set temperature for creative output
|
| 17 |
+
max_retries=2 # Set maximum retries for requests
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Function to generate quiz questions
|
| 21 |
+
def generate_questions(topic="math", num_questions=3):
|
| 22 |
+
"""
|
| 23 |
+
Generate quiz questions without answers.
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
prompt = f"Generate {num_questions} {topic} multiple-choice quiz questions with 4 options each. Do not include answers."
|
| 27 |
+
response = llm.invoke([
|
| 28 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 29 |
+
{"role": "user", "content": prompt}
|
| 30 |
+
])
|
| 31 |
+
content = response.content if hasattr(response, 'content') else str(response)
|
| 32 |
+
|
| 33 |
+
# Debug: Print raw content
|
| 34 |
+
print("Raw Questions Content:", content)
|
| 35 |
+
|
| 36 |
+
# Parse questions
|
| 37 |
+
questions = []
|
| 38 |
+
current_question = {"options": []}
|
| 39 |
+
|
| 40 |
+
for line in content.split("\n"):
|
| 41 |
+
line = line.strip()
|
| 42 |
+
if line.startswith("1.") or line.startswith("2.") or line.startswith("3."):
|
| 43 |
+
# Save the previous question if present
|
| 44 |
+
if current_question and "question" in current_question:
|
| 45 |
+
questions.append(current_question)
|
| 46 |
+
current_question = {"options": []}
|
| 47 |
+
|
| 48 |
+
current_question["question"] = line
|
| 49 |
+
|
| 50 |
+
elif line.startswith("A)") or line.startswith("B)") or line.startswith("C)") or line.startswith("D)"):
|
| 51 |
+
# Append options
|
| 52 |
+
current_question["options"].append(line)
|
| 53 |
+
|
| 54 |
+
# Add the last question if it exists
|
| 55 |
+
if current_question and "question" in current_question:
|
| 56 |
+
questions.append(current_question)
|
| 57 |
+
|
| 58 |
+
return questions if questions else []
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"Error generating questions: {e}")
|
| 61 |
+
return []
|
| 62 |
+
|
| 63 |
+
# Function to fetch concise answers (A, B, C, D) for the provided questions
|
| 64 |
+
def fetch_answers(questions):
|
| 65 |
+
"""
|
| 66 |
+
Fetch concise answers (A, B, C, or D) for the provided questions.
|
| 67 |
+
"""
|
| 68 |
+
try:
|
| 69 |
+
prompt = "Provide the correct answers for the following multiple-choice questions in concise form (e.g., A, B, C, or D). Only return the letter of the correct answer for each question:\n"
|
| 70 |
+
for i, question in enumerate(questions):
|
| 71 |
+
prompt += f"\n{i + 1}. {question['question']}\n"
|
| 72 |
+
for option in question["options"]:
|
| 73 |
+
prompt += f"{option}\n"
|
| 74 |
+
|
| 75 |
+
response = llm.invoke([
|
| 76 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 77 |
+
{"role": "user", "content": prompt}
|
| 78 |
+
])
|
| 79 |
+
content = response.content if hasattr(response, 'content') else str(response)
|
| 80 |
+
|
| 81 |
+
# Debug: Print raw answers content
|
| 82 |
+
print("Raw Answers Content:", content)
|
| 83 |
+
|
| 84 |
+
# Extract concise answers (e.g., A, B, C, or D)
|
| 85 |
+
answers = {}
|
| 86 |
+
for line in content.split("\n"):
|
| 87 |
+
line = line.strip()
|
| 88 |
+
if line and len(line) == 1 and line.upper() in ["A", "B", "C", "D"]:
|
| 89 |
+
question_number = len(answers) # Match order with the question index
|
| 90 |
+
answers[question_number] = line.upper()
|
| 91 |
+
|
| 92 |
+
return answers
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.error(f"Error fetching answers: {e}")
|
| 95 |
+
return {}
|
| 96 |
+
|
| 97 |
+
# Initialize session state
|
| 98 |
+
if "score" not in st.session_state:
|
| 99 |
+
st.session_state.score = 0
|
| 100 |
+
if "current_question" not in st.session_state:
|
| 101 |
+
st.session_state.current_question = 0
|
| 102 |
+
if "attempts" not in st.session_state:
|
| 103 |
+
st.session_state.attempts = 0
|
| 104 |
+
if "lives" not in st.session_state:
|
| 105 |
+
st.session_state.lives = 3
|
| 106 |
+
if "questions" not in st.session_state or not st.session_state.questions:
|
| 107 |
+
st.session_state.questions = generate_questions("math", num_questions=3)
|
| 108 |
+
if "answers" not in st.session_state or not st.session_state.answers:
|
| 109 |
+
st.session_state.answers = fetch_answers(st.session_state.questions)
|
| 110 |
+
|
| 111 |
+
# Function to reset the quiz
|
| 112 |
+
def reset_quiz():
|
| 113 |
+
st.session_state.current_question = 0
|
| 114 |
+
st.session_state.score = 0
|
| 115 |
+
st.session_state.attempts = 0
|
| 116 |
+
st.session_state.lives = 3
|
| 117 |
+
st.session_state.questions = generate_questions("math", num_questions=3)
|
| 118 |
+
st.session_state.answers = fetch_answers(st.session_state.questions)
|
| 119 |
+
|
| 120 |
+
# Main quiz logic
|
| 121 |
+
st.title("Dino Game - Quiz Mode")
|
| 122 |
+
st.write("Answer the questions to regain lives!")
|
| 123 |
+
|
| 124 |
+
if st.session_state.lives > 0: # Check if the player has lives left
|
| 125 |
+
if st.session_state.current_question < len(st.session_state.questions):
|
| 126 |
+
current_quiz = st.session_state.questions[st.session_state.current_question]
|
| 127 |
+
correct_answer = st.session_state.answers.get(st.session_state.current_question, "").lower()
|
| 128 |
+
|
| 129 |
+
# Display the question and options
|
| 130 |
+
st.write(f"**Question:** {current_quiz['question']}")
|
| 131 |
+
for option in current_quiz["options"]:
|
| 132 |
+
st.write(option)
|
| 133 |
+
|
| 134 |
+
# Input for user answer
|
| 135 |
+
user_answer = st.text_input("Your Answer (e.g., A, B, C, or D)", key=f"q{st.session_state.current_question}")
|
| 136 |
+
|
| 137 |
+
if st.button("Submit", key=f"submit_{st.session_state.current_question}"):
|
| 138 |
+
if user_answer:
|
| 139 |
+
print("correct answer", correct_answer)
|
| 140 |
+
print("user answer", user_answer.lower())
|
| 141 |
+
if user_answer.lower() == correct_answer: # Compare with the correct answer
|
| 142 |
+
st.success("Correct!")
|
| 143 |
+
st.session_state.score += 1
|
| 144 |
+
st.session_state.current_question += 1
|
| 145 |
+
st.session_state.attempts = 0
|
| 146 |
+
else:
|
| 147 |
+
st.error("Wrong! Try again.")
|
| 148 |
+
st.session_state.attempts += 1
|
| 149 |
+
if st.session_state.attempts >= 3: # Max retries reached
|
| 150 |
+
st.warning("Maximum attempts reached! Moving to the next question.")
|
| 151 |
+
st.session_state.lives -= 1 # Decrease life if wrong attempts exceed limit
|
| 152 |
+
st.session_state.current_question += 1
|
| 153 |
+
st.session_state.attempts = 0
|
| 154 |
+
else:
|
| 155 |
+
st.warning("Please enter an answer before submitting.")
|
| 156 |
+
else:
|
| 157 |
+
st.write(f"Quiz completed! **Score:** {st.session_state.score}/{len(st.session_state.questions)}")
|
| 158 |
+
if st.session_state.score == len(st.session_state.questions):
|
| 159 |
+
st.write("You answered all questions correctly! Restoring lives.")
|
| 160 |
+
st.session_state.lives = 3 # Restore lives if all questions were answered correctly
|
| 161 |
+
if st.button("Restart Quiz"):
|
| 162 |
+
reset_quiz()
|
| 163 |
+
else:
|
| 164 |
+
st.write("You have no lives left! Returning to the main game.")
|
| 165 |
+
if st.button("Exit Quiz Mode"):
|
| 166 |
+
# Reset quiz and return to main game
|
| 167 |
+
reset_quiz()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain-groq
|
| 3 |
+
python-dotenv
|