import gradio as gr import game import ai from app import format_game_result def get_system_message(game_mode): return """You are a friendly classroom tutor for 9-10 year-olds. Generate one multiple-choice geography question that a 4th-grader would answer. Requirements: 1. Question text - one short sentence (or two) that asks the student to identify a place, landmark, or simple map feature. 2. Four answer options labeled A)-D). All options should be plausible country, state, or landmark names. 3. After the question and choices, include a short "Answer key" line that states the correct answer (e.g., "Correct answer: A"). 4. If a student gives an incorrect answer, respond with: "That's okay! Let's look at why that answer isn't right and see what clues the question gives us. Remember, …" Then provide a brief explanation (1-2 sentences) that points the student toward the correct answer without giving it away outright. 5. Keep vocabulary simple, concrete, and age-appropriate. 6. Do not mention the word "multiple choice" in the output; just give the question, options, answer key, and the incorrect-answer reaction. Example output (use as a guide only, do NOT copy): **Question:** Which country is shaped like a boot and is located in southern Europe? A) France B) Italy C) Greece D) Spain **Answer key:** Correct answer: B If the student answers incorrectly: "That's okay! Let's look at why that answer isn't right and see what clues the question gives us. Remember, the question mentions the country looks like a boot. Think about which country on a map of Europe really looks like you could wear it on your foot!" """ def respond( message, history: list[dict[str, str]], max_tokens, game_mode_selection, ): """ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference """ # If this is the start of a new conversation (empty history), generate a new country/state if not history: game.guess_number = 0 game.current_system = get_system_message(game.MODE_STATES) print(f"🔍 DEBUG - New session started, selected state: {game.selected_country}") game.guess_number += 1 messages = [{"role": "system", "content": game.current_system + str(game.guess_number)}] messages.append({"role": "user", "content": message}) # Debug: Calculate approximate input token count total_input_chars = sum(len(str(msg.get("content", ""))) for msg in messages) estimated_input_tokens = total_input_chars // 4 # Rough approximation: 4 chars per token print(f"🔍 DEBUG - Estimated input tokens: {estimated_input_tokens}") print(f"🔍 DEBUG - Messages count: {len(messages)}") print(f"🔍 DEBUG - Max tokens setting: {max_tokens}") # Debug: Show each message type and length for i, msg in enumerate(messages): role = msg.get("role", "unknown") content = str(msg.get("content", "")) print(f"🔍 DEBUG - Message {i+1} ({role}): {len(content)} chars") if role == "system": print(f"🔍 DEBUG - System message preview: ...{content[-100:]}") elif role == "user": print(f"🔍 DEBUG - User message: {content}") elif role == "assistant": print(f"🔍 DEBUG - Assistant message: {content[:50]}...") response = "" output_token_count = 0 try: for message_chunk in ai.client.chat_completion( messages, stream=True, response_format={"type": "text"}, ): choices = message_chunk.choices token = "" if len(choices) and choices[0].delta.content: token = choices[0].delta.content output_token_count += 1 response += token # Debug: Show output token statistics estimated_output_tokens = len(response) // 4 # Rough approximation print(f"🔍 DEBUG - Output token chunks received: {output_token_count}") print(f"🔍 DEBUG - Estimated output tokens (by chars): {estimated_output_tokens}") print(f"🔍 DEBUG - Response length: {len(response)} characters") print(f"🔍 DEBUG - Raw response: {response}") # Clean the response to remove unwanted artifacts response = ai.clean_response(response) print(f"🔍 DEBUG - Cleaned response: {response}") # Check if this is a game end response and format it nicely if "The country was" in response or "The state was" in response: print(f"🔍 DEBUG - Game end detected! Location extracted: {game.selected_country}") return format_game_result(response) elif game.guess_number == 20: print(f"🔍 DEBUG - Maximum guesses reached: {game.guess_number}") return format_game_result(response) else: print("🔍 DEBUG - Regular response (no game end)") return response except Exception as e: return f"Error during inference: {str(e)}"