🏆 Scoreboard 🏆
{{ scoreboard }}
app.py # Modify the generate_scoreboard function to include sorting options and count of incorrect answers def generate_scoreboard(sort_by='date'): try: # Assuming you have access to the history of responses history_data = state["responses"] # Calculate the number of correct and incorrect answers correct_count = sum(1 for entry in history_data if entry['feedback'].lower() == "correct") incorrect_count = sum(1 for entry in history_data if entry['feedback'].lower() == "incorrect") total_count = len(history_data) # Calculate the percentage of correct answers percentage_correct = (correct_count / total_count) * 100 if total_count > 0 else 0 # Sort the history data based on the selected criteria if sort_by == 'correctness': sorted_history = sorted(history_data, key=lambda x: x['feedback'], reverse=True) elif sort_by == 'difficulty': sorted_history = sorted(history_data, key=lambda x: len(x['question']), reverse=True) # Example: Sorting by question length else: sorted_history = history_data # Default: Sort by date # Create a prompt for generating the scoreboard prompt = f"Generate a scoreboard based on the following history:\n\nTotal Questions: {total_count}\nCorrect Answers: {correct_count}\nIncorrect Answers: {incorrect_count}\nPercentage Correct: {percentage_correct:.2f}%\n\n" for entry in sorted_history: prompt += f"Question: {entry['question']}\nAnswer: {entry['answer']}\nFeedback: {entry['feedback']}\n\n" # Use the v1/chat/completions endpoint for chat models response = openai.ChatCompletion.create( model="gpt-3.5-turbo", # Adjust the model as needed messages=[{"role": "user", "content": prompt}], max_tokens=200 ) # Extract and return the generated scoreboard scoreboard = response.choices[0].message['content'] return scoreboard except Exception as e: return f"Error generating scoreboard: {str(e)}" # Route to display the scoreboard with sorting options @app.route('/scoreboard') def scoreboard(): try: # Get the sorting parameter from the request sort_by = request.args.get('sort_by', 'date') # Generate the scoreboard with the specified sorting option scoreboard_data = generate_scoreboard(sort_by) # Render the scoreboard template with the generated scoreboard data and sorting options return render_template('scoreboard.html', scoreboard=scoreboard_data, sort_by=sort_by) except Exception as e: return f"Error displaying scoreboard: {str(e)}" scoreboard.html
{{ scoreboard }}