import gradio as gr from groq import Groq import os from PIL import Image, ImageDraw, ImageFont, ImageFilter from datetime import datetime import json import tempfile from typing import List, Dict, Tuple, Optional from dataclasses import dataclass import subprocess import re import random @dataclass class Question: question: str options: List[str] correct_answer: int @dataclass class QuizFeedback: is_correct: bool selected: Optional[str] correct_answer: str class QuizGenerator: def __init__(self, api_key: str): self.client = Groq(api_key=api_key) def generate_questions(self, text: str, num_questions: int) -> List[Question]: """Generate quiz questions using gemma2-9b-it""" prompt = self._create_prompt(text, num_questions) try: response = self.client.chat.completions.create( messages=[ { "role": "system", "content": "You are a quiz generator. Generate multiple choice questions that are clear and focused." }, { "role": "user", "content": prompt } ], model="gemma2-9b-it", temperature=0, max_tokens=6000 ) # Extract content safely content = response.choices[0].message.content if not content: raise ValueError("Empty response content") # Parse and validate questions questions = self._parse_response(content) validated = self._validate_questions(questions, num_questions) # Check if we have exactly the requested number of questions if len(validated) < num_questions: # Generate additional questions if needed additional_needed = num_questions - len(validated) additional_questions = self.generate_questions(text, additional_needed) validated.extend(additional_questions) elif len(validated) > num_questions: # Trim to exact number needed validated = validated[:num_questions] if len(validated) != num_questions: raise ValueError(f"Failed to generate exactly {num_questions} questions") return validated except Exception as e: print(f"Error in generate_questions: {str(e)}") if 'response' in locals(): print("Response content:", content if 'content' in locals() else None) raise QuizGenerationError(f"Failed to generate questions: {str(e)}") def _validate_questions(self, questions: List[Dict], num_questions: int) -> List[Question]: """Validate questions with strict checking and ensure correct number""" validated = [] for q in questions: try: # Skip invalid questions if not isinstance(q, dict): continue # Check required fields if not all(key in q for key in ['question', 'options', 'correct_answer']): continue # Validate options if not isinstance(q['options'], list) or len(q['options']) != 4: continue # Validate correct_answer try: correct_idx = int(q['correct_answer']) if not 0 <= correct_idx <= 3: continue except (ValueError, TypeError): continue # Validate question content if not q['question'].strip() or any(not opt.strip() for opt in q['options']): continue # Create validated Question object validated.append(Question( question=str(q['question']).strip(), options=[str(opt).strip() for opt in q['options']], correct_answer=correct_idx )) except Exception as e: print(f"Validation error: {str(e)}") continue # Stop if we have enough validated questions if len(validated) == num_questions: break return validated def _create_prompt(self, text: str, num_questions: int) -> str: """Create a simple, clear prompt optimized for gemma2-9b-it with emphasis on exact number""" return f"""Create exactly {num_questions} multiple choice questions about this text. Return only the JSON array in this exact format: [ {{ "question": "Write the question here?", "options": [ "First option", "Second option", "Third option", "Fourth option" ], "correct_answer": 0 }} ] Rules: 1. Return only the JSON array 2. Generate exactly {num_questions} questions 3. Each question must have exactly 4 options 4. correct_answer must be 0, 1, 2, or 3 5. No explanations or additional text Text to use: {text.strip()}""" def _parse_response(self, response_text: str) -> List[Dict]: """Parse response with improved error handling""" try: # Clean up the response text cleaned = response_text.strip() # Remove any markdown formatting cleaned = cleaned.replace('```json', '').replace('```', '').strip() # Find the JSON array start = cleaned.find('[') end = cleaned.rfind(']') + 1 if start == -1 or end == 0: raise ValueError("No JSON array found in response") json_str = cleaned[start:end] # Remove any trailing commas before closing brackets json_str = re.sub(r',(\s*})', r'\1', json_str) json_str = re.sub(r',(\s*])', r'\1', json_str) # Try to parse the cleaned JSON try: return json.loads(json_str) except json.JSONDecodeError: # If that fails, try using ast.literal_eval as a fallback import ast return ast.literal_eval(json_str) except Exception as e: print(f"Parse error details: {str(e)}") print(f"Attempted to parse: {response_text}") # Last resort: try to fix the JSON manually try: # Remove any trailing commas and fix newlines fixed = re.sub(r',(\s*[}\]])', r'\1', response_text) fixed = fixed.replace('}\n{', '},{') fixed = fixed.strip() if not fixed.startswith('['): fixed = '[' + fixed if not fixed.endswith(']'): fixed = fixed + ']' return json.loads(fixed) except: raise ValueError(f"Failed to parse response: {str(e)}") def _validate_questions(self, questions: List[Dict], num_questions: int) -> List[Question]: """Validate questions with strict checking""" validated = [] for q in questions[:num_questions]: try: # Skip invalid questions if not isinstance(q, dict): continue # Check required fields if not all(key in q for key in ['question', 'options', 'correct_answer']): continue # Validate options if not isinstance(q['options'], list) or len(q['options']) != 4: continue # Validate correct_answer try: correct_idx = int(q['correct_answer']) if not 0 <= correct_idx <= 3: continue except (ValueError, TypeError): continue # Create validated Question object validated.append(Question( question=str(q['question']).strip(), options=[str(opt).strip() for opt in q['options']], correct_answer=correct_idx )) except Exception as e: print(f"Validation error: {str(e)}") continue return validated def _is_valid_json(self, json_str: str) -> bool: """Check if a string is valid JSON""" try: json.loads(json_str) return True except: return False class FontManager: """Manages font installation and loading for the certificate generator""" @staticmethod def install_fonts(): """Install required fonts if they're not already present""" try: # Install fonts package subprocess.run([ "apt-get", "update", "-y" ], check=True) subprocess.run([ "apt-get", "install", "-y", "fonts-liberation", # Liberation Sans fonts "fontconfig", # Font configuration "fonts-dejavu-core" # DejaVu fonts as fallback ], check=True) # Clear font cache subprocess.run(["fc-cache", "-f"], check=True) print("Fonts installed successfully") except subprocess.CalledProcessError as e: print(f"Warning: Could not install fonts: {e}") except Exception as e: print(f"Warning: Unexpected error installing fonts: {e}") @staticmethod def get_font_paths() -> Dict[str, str]: """Get the paths to the required fonts with multiple fallbacks""" standard_paths = [ "/usr/share/fonts", "/usr/local/share/fonts", "/usr/share/fonts/truetype", "~/.fonts" ] font_paths = { 'regular': None, 'bold': None } # Common font filenames to try fonts_to_try = { 'regular': [ 'LiberationSans-Regular.ttf', 'DejaVuSans.ttf', 'FreeSans.ttf' ], 'bold': [ 'LiberationSans-Bold.ttf', 'DejaVuSans-Bold.ttf', 'FreeSans-Bold.ttf' ] } def find_font(font_name: str) -> Optional[str]: """Search for a font file in standard locations""" for base_path in standard_paths: for root, _, files in os.walk(os.path.expanduser(base_path)): if font_name in files: return os.path.join(root, font_name) return None # Try to find each font for style in ['regular', 'bold']: for font_name in fonts_to_try[style]: font_path = find_font(font_name) if font_path: font_paths[style] = font_path break # If no fonts found, try using fc-match as fallback if not all(font_paths.values()): try: for style in ['regular', 'bold']: if not font_paths[style]: result = subprocess.run( ['fc-match', '-f', '%{file}', 'sans-serif:style=' + style], capture_output=True, text=True ) if result.returncode == 0 and result.stdout.strip(): font_paths[style] = result.stdout.strip() except Exception as e: print(f"Warning: Could not use fc-match to find fonts: {e}") return font_paths class QuizGenerationError(Exception): """Exception raised for errors in quiz generation""" pass class CertificateGenerator: def __init__(self): self.certificate_size = (1200, 800) self.background_color = '#FFFFFF' self.border_color = '#1C1D1F' # Install fonts if needed FontManager.install_fonts() self.font_paths = FontManager.get_font_paths() def _load_fonts(self) -> Dict[str, ImageFont.FreeTypeFont]: """Load fonts with fallbacks""" fonts = {} try: if self.font_paths['regular'] and self.font_paths['bold']: fonts['title'] = ImageFont.truetype(self.font_paths['bold'], 36) fonts['subtitle'] = ImageFont.truetype(self.font_paths['regular'], 14) fonts['text'] = ImageFont.truetype(self.font_paths['regular'], 20) fonts['name'] = ImageFont.truetype(self.font_paths['bold'], 32) else: raise ValueError("No suitable fonts found") except Exception as e: print(f"Font loading error: {e}. Using default font.") default = ImageFont.load_default() fonts = { 'title': default, 'subtitle': default, 'text': default, 'name': default } return fonts def _add_professional_border(self, draw: ImageDraw.Draw): """Add professional border with improved corners""" padding = 40 border_width = 2 corner_radius = 10 # Draw rounded rectangle border x0, y0 = padding, padding x1, y1 = self.certificate_size[0] - padding, self.certificate_size[1] - padding # Draw corners draw.arc((x0, y0, x0 + corner_radius * 2, y0 + corner_radius * 2), 180, 270, '#1C1D1F', border_width) draw.arc((x1 - corner_radius * 2, y0, x1, y0 + corner_radius * 2), 270, 0, '#1C1D1F', border_width) draw.arc((x0, y1 - corner_radius * 2, x0 + corner_radius * 2, y1), 90, 180, '#1C1D1F', border_width) draw.arc((x1 - corner_radius * 2, y1 - corner_radius * 2, x1, y1), 0, 90, '#1C1D1F', border_width) # Draw lines draw.line((x0 + corner_radius, y0, x1 - corner_radius, y0), '#1C1D1F', border_width) # Top draw.line((x0 + corner_radius, y1, x1 - corner_radius, y1), '#1C1D1F', border_width) # Bottom draw.line((x0, y0 + corner_radius, x0, y1 - corner_radius), '#1C1D1F', border_width) # Left draw.line((x1, y0 + corner_radius, x1, y1 - corner_radius), '#1C1D1F', border_width) # Right def _add_content( self, draw: ImageDraw.Draw, fonts: Dict[str, ImageFont.FreeTypeFont], name: str, course_name: str, score: float, y_offset: int = 140 ): """Add content with adjusted vertical positioning""" # Add "CERTIFICATE OF COMPLETION" text draw.text((60, y_offset), "CERTIFICATE OF COMPLETION", font=fonts['subtitle'], fill='#666666') # Add course name (large and bold) course_name = course_name.strip() or "Assessment" draw.text((60, y_offset + 60), course_name, font=fonts['title'], fill='#1C1D1F') # Add instructor info draw.text((60, y_offset + 160), "Instructor", font=fonts['subtitle'], fill='#666666') draw.text((60, y_offset + 190), "AI Teacher", font=fonts['text'], fill='#1C1D1F') # Add participant name (large) name = name.strip() or "Participant" draw.text((60, y_offset + 280), name, font=fonts['name'], fill='#1C1D1F') # Add date and score info date_str = datetime.now().strftime("%b. %d, %Y") # Date section draw.text((60, y_offset + 360), "Date", font=fonts['subtitle'], fill='#666666') draw.text((60, y_offset + 390), date_str, font=fonts['text'], fill='#1C1D1F') # Score section draw.text((300, y_offset + 360), "Score", font=fonts['subtitle'], fill='#666666') draw.text((300, y_offset + 390), f"{float(score):.1f}%", font=fonts['text'], fill='#1C1D1F') # Footer section certificate_id = f"Certificate no: {datetime.now().strftime('%Y%m%d')}-{abs(hash(name)) % 10000:04d}" ref_number = f"Reference Number: {abs(hash(name + date_str)) % 10000:04d}" draw.text((60, 720), certificate_id, font=fonts['subtitle'], fill='#666666') draw.text((1140, 720), ref_number, font=fonts['subtitle'], fill='#666666', anchor="ra") def _add_logo(self, certificate: Image.Image, logo_path: str): try: logo = Image.open(logo_path) # Resize logo to appropriate size logo.thumbnail((150, 80)) # Position in top-left corner with padding certificate.paste(logo, (60, 50), mask=logo if 'A' in logo.getbands() else None) except Exception as e: print(f"Error adding logo: {e}") def _add_photo(self, certificate: Image.Image, photo_path: str): """Add a clear circular profile photo in the top-right corner with adjusted position""" try: if not photo_path or not os.path.exists(photo_path): print(f"Photo path does not exist: {photo_path}") return # Open and process photo photo = Image.open(photo_path) # Define size for circular photo size = (120, 120) # Convert to RGB if not already if photo.mode not in ('RGB', 'RGBA'): photo = photo.convert('RGB') # Create high-quality circular mask mask = Image.new('L', size, 0) draw = ImageDraw.Draw(mask) draw.ellipse((0, 0, size[0], size[1]), fill=255) # Resize photo maintaining aspect ratio aspect = photo.width / photo.height if aspect > 1: new_height = size[1] new_width = int(new_height * aspect) else: new_width = size[0] new_height = int(new_width / aspect) photo = photo.resize((new_width, max(new_height, 1)), Image.Resampling.LANCZOS) # Center crop if aspect > 1: left = (new_width - size[0]) // 2 photo = photo.crop((left, 0, left + size[0], size[1])) else: top = (new_height - size[1]) // 2 photo = photo.crop((0, top, size[0], top + size[1])) # Create circular photo output = Image.new('RGBA', size, (0, 0, 0, 0)) output.paste(photo, (0, 0)) output.putalpha(mask) # Adjusted position - moved down from top photo_x = certificate.width - size[0] - 60 # 60px from right photo_y = 50 # Increased from 40 to 50px from top # Add white background circle bg = Image.new('RGBA', size, (255, 255, 255, 255)) certificate.paste(bg, (photo_x, photo_y), mask=mask) # Paste the photo certificate.paste(output, (photo_x, photo_y), mask=output) print(f"Successfully added photo at position ({photo_x}, {photo_y})") except Exception as e: print(f"Error adding photo: {str(e)}") import traceback traceback.print_exc() def generate( self, score: float, name: str, course_name: str, company_logo: Optional[str] = None, participant_photo: Optional[str] = None ) -> str: """Generate certificate with improved photo handling""" try: # Create base certificate certificate = Image.new('RGB', self.certificate_size, self.background_color) draw = ImageDraw.Draw(certificate) # Add border self._add_professional_border(draw) # Load fonts fonts = self._load_fonts() # Add company logo if provided if company_logo and os.path.exists(company_logo): self._add_logo(certificate, company_logo) # Add participant photo if provided if participant_photo: print(f"Processing photo: {participant_photo}") # Debug info self._add_photo(certificate, participant_photo) # Add content self._add_content(draw, fonts, str(name), str(course_name), float(score)) # Save certificate return self._save_certificate(certificate) except Exception as e: print(f"Error generating certificate: {str(e)}") import traceback traceback.print_exc() return None def _create_base_certificate(self) -> Image.Image: """Create base certificate with improved background""" # Create base image certificate = Image.new('RGB', self.certificate_size, self.background_color) # Add subtle gradient background (optional) draw = ImageDraw.Draw(certificate) # Add very subtle grain texture for professional look (optional) width, height = certificate.size for x in range(0, width, 4): for y in range(0, height, 4): if random.random() > 0.5: draw.point((x, y), fill=(250, 250, 250)) return certificate def _save_certificate(self, certificate: Image.Image) -> str: """Save certificate with improved error handling""" try: temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png') certificate.save(temp_file.name, 'PNG', quality=95) print(f"Certificate saved to: {temp_file.name}") # Debug info return temp_file.name except Exception as e: print(f"Error saving certificate: {str(e)}") return None class QuizApp: def __init__(self, api_key: str): self.quiz_generator = QuizGenerator(api_key) self.certificate_generator = CertificateGenerator() self.current_questions: List[Question] = [] self.logo_path = "Logo.png" self.selected_level = "Basic" # Default level # Map difficulty levels to number of questions self.difficulty_levels = { "Basic": 5, "Intermediate": 10, "Advanced": 20 } # Add fixed content here self.fixed_content = """The Earth's natural resources form the foundation of human civilization, providing essential materials for survival and industrial development. These resources are categorized into renewable and non-renewable types, with fossil fuels representing crucial non-renewable energy sources. Coal and petroleum, formed over millions of years from decomposed organic matter, underwent complex geological processes under specific conditions of temperature and pressure. Coal formation occurred primarily from plant matter in ancient swamps, while petroleum developed from marine organisms in prehistoric seas. The extraction of these resources involves sophisticated mining and drilling operations, with coal obtained through surface or underground mining techniques depending on deposit depth. Petroleum extraction requires advanced drilling technology, often extending thousands of meters below the Earth's surface. These fossil fuels serve multiple purposes, functioning as energy sources and raw materials for various industrial processes, though their combustion releases greenhouse gases and other pollutants into the atmosphere, contributing to environmental challenges including climate change and air quality degradation. The process of combustion represents a fundamental chemical reaction involving fuel and oxygen, producing heat and light energy. Different types of combustion exist, including rapid combustion, spontaneous combustion, and explosive combustion, each characterized by specific reaction rates and energy release patterns. A flame's structure exhibits distinct zones with varying temperatures and chemical compositions. The innermost zone contains unburned fuel vapors, while the middle zone hosts active combustion reactions. The outer zone represents the complete combustion region, where maximum temperature occurs. Fuels vary in their combustion efficiency and energy output, with different materials requiring specific conditions for ignition and sustained burning. Fire safety protocols emphasize prevention and control measures, including proper fuel storage, ventilation requirements, and emergency response procedures. Living organisms display remarkable diversity in their reproductive strategies, ensuring species continuation through various mechanisms. Sexual reproduction, prevalent in complex organisms, involves the fusion of male and female gametes through fertilization. This process combines genetic material from both parents, producing offspring with unique genetic combinations that contribute to species adaptability and evolution. Reproductive systems in different animal groups show specialized adaptations, from external fertilization in aquatic organisms to internal fertilization in terrestrial species. Mammals exhibit particularly complex reproductive processes, including internal fertilization, embryonic development within the uterus, and post-birth parental care. Asexual reproduction, observed in simpler organisms, creates genetically identical offspring from a single parent through processes like budding, fragmentation, and binary fission. This reproductive method enables rapid population growth under favorable conditions but limits genetic diversity. Adolescence represents a critical developmental period characterized by significant physical, psychological, and social changes. This transition phase, typically occurring between ages 11 and 19, involves complex hormonal interactions orchestrated by the endocrine system. The hypothalamus and pituitary gland initiate puberty by releasing hormones that stimulate the gonads to produce sex hormones. These hormones trigger the development of secondary sexual characteristics, including voice changes, body hair growth, and reproductive organ maturation. Growth hormones promote rapid physical development, resulting in increased height, muscle mass, and bone density. Emotional and psychological changes accompany physical development, influenced by both hormonal fluctuations and social factors. Understanding these changes helps adolescents navigate this challenging period while maintaining physical and mental well-being. Forces govern all physical interactions in the universe, from subatomic particles to celestial bodies. Different types of forces include contact forces, requiring direct physical interaction, and non-contact forces, operating through fields. Gravitational force, a fundamental non-contact force, attracts all objects with mass toward each other, determining planetary motions and everyday phenomena like falling objects. Electromagnetic forces, responsible for atomic and molecular interactions, include both attractive and repulsive components. Pressure, representing force per unit area, manifests in various natural and technological contexts. Atmospheric pressure results from air molecule collisions, varying with altitude and weather conditions. Liquid pressure increases linearly with depth due to fluid weight, following Pascal's principle of pressure transmission. Understanding pressure principles enables technologies ranging from hydraulic systems to atmospheric pressure applications. Friction arises from surface irregularities and molecular interactions between objects in contact. The nature and magnitude of frictional forces depend on surface characteristics, normal force, and relative motion states. Static friction prevents relative motion between stationary surfaces, while kinetic friction opposes motion between sliding surfaces. Rolling friction, typically lower than sliding friction, occurs with wheeled or spherical objects. Friction plays both beneficial and detrimental roles in daily life and technological applications. Beneficial aspects include enabling walking, writing, and mechanical grip, while disadvantages include energy loss, wear, and reduced efficiency in machines. Various methods modify friction, including lubrication, surface treatment, and material selection, optimizing it for specific applications. Sound represents a mechanical wave phenomenon requiring a material medium for propagation. These waves transfer energy through matter via alternating compression and rarefaction regions. Sound wave characteristics determine audible properties, with frequency determining pitch and amplitude affecting loudness. The human auditory range spans approximately 20 Hz to 20,000 Hz, though this varies with age and individual factors. Sound waves exhibit behaviors including reflection, refraction, and diffraction. Reflection produces echoes and enables technologies like sonar and ultrasound imaging. Musical instruments generate sound through various mechanisms, including vibrating strings, air columns, and membranes, each producing characteristic timbres through harmonic combinations. Understanding sound principles enables acoustic design, noise control, and audio technology development. Chemical effects of electric current demonstrate the interrelation between electrical and chemical processes. Electrolysis represents a fundamental electrochemical process where electrical energy drives chemical reactions, decomposing compounds into constituent elements. This process requires an electrolyte solution conducting electricity through ion movement, with electrodes serving as sites for oxidation and reduction reactions. Electroplating applications utilize electrolysis principles to deposit metal coatings on objects, providing decorative finishes or corrosion protection. Factors affecting electroplating include current density, solution concentration, and temperature. Industrial applications extend to metal purification, electrochemical manufacturing, and battery technology. Understanding these principles enables various practical applications in metallurgy, electronics, and chemical processing. Natural phenomena encompass various geological and atmospheric events shaping Earth's environment. Lightning results from charge separation in clouds, creating potential differences leading to electrical discharge. This process involves complex atmospheric dynamics, including updrafts, precipitation, and charge distribution. Thunder accompanies lightning due to rapid air expansion from intense heating. Earthquakes originate from tectonic plate movements, releasing stored energy as seismic waves. Primary waves (P-waves) involve compression and rarefaction, while secondary waves (S-waves) create perpendicular displacements. Surface waves cause the most significant damage to structures. Understanding these phenomena enables better prediction, protection, and response strategies for natural disasters. Light exhibits complex behavior explained through both wave and particle models. As electromagnetic radiation, light propagates through space at approximately 3×10⁸ meters per second in vacuum. When encountering matter, light undergoes various interactions including reflection, refraction, dispersion, and absorption. Reflection follows specific laws relating incident and reflected angles, enabling mirror applications and optical instruments. Refraction occurs at medium boundaries due to velocity changes, causing light ray deviation. This principle explains various optical phenomena including rainbow formation and lens operation. Different materials exhibit varying optical properties, including refractive index and transparency, influencing light behavior. Understanding these principles enables technologies ranging from fiber optics to solar cells. Microorganisms comprise diverse microscopic life forms including bacteria, viruses, fungi, and protozoa. These organisms play crucial ecological roles in nutrient cycling, decomposition, and maintaining environmental balance. Beneficial microorganisms contribute to food production through fermentation, nitrogen fixation in agriculture, and waste treatment processes. Harmful microorganisms cause diseases through various mechanisms including infection, toxin production, and immune system disruption. Proper sanitation, food handling, and personal hygiene help prevent microbial infections. Modern biotechnology utilizes microorganisms for producing medicines, enzymes, and other valuable products. Understanding microbial biology enables better disease control and beneficial application development. Crop production involves systematic agricultural practices optimizing plant growth and yield. Modern farming combines traditional knowledge with scientific methods, employing mechanization and precision agriculture techniques. Soil preparation includes plowing, leveling, and nutrient management through organic and inorganic fertilizers. Seed selection considers factors including climate adaptation, disease resistance, and yield potential. Irrigation methods range from surface flooding to efficient drip systems, optimizing water usage. Crop protection involves integrated pest management combining biological, chemical, and cultural control methods. Harvesting timing and techniques affect crop quality and storage stability. Sustainable agriculture practices balance productivity with environmental conservation. Biodiversity conservation requires comprehensive strategies protecting species and ecosystems. Protected areas establish legal frameworks for habitat preservation, while species recovery programs target endangered populations. Human activities including urbanization, pollution, and climate change threaten biodiversity through habitat destruction and ecosystem disruption. Conservation approaches include both in-situ protection in natural habitats and ex-situ preservation through botanical gardens and seed banks. Ecosystem restoration projects aim to rehabilitate damaged environments, while public awareness programs promote conservation understanding and support. International agreements and national legislation provide legal frameworks for biodiversity protection. Matter exists in various states with distinct physical and chemical properties. Atomic structure determines chemical behavior, with electrons participating in bonding and reactions. Chemical reactions involve breaking and forming bonds, often accompanied by energy changes. Different reaction types include combination, decomposition, displacement, and double displacement reactions. Factors affecting reaction rates include temperature, concentration, surface area, and catalysts. Understanding chemical principles enables material development and process optimization across industries. Energy transformations occur continuously in natural and artificial systems. Various energy forms include mechanical, electrical, thermal, chemical, and nuclear energy. Energy conservation principles state that energy cannot be created or destroyed, only converted between forms. Energy efficiency measures how effectively energy converts between forms, with some energy always lost as heat. Renewable energy sources including solar, wind, and hydroelectric power offer sustainable alternatives to fossil fuels. Energy conservation strategies reduce consumption and environmental impact through improved efficiency and behavioral changes. Climate and weather patterns result from complex atmospheric and oceanic interactions. Air pressure differences drive wind patterns, while water cycle processes create precipitation. Global climate systems influence regional weather conditions through air mass movements and ocean currents. Climate change affects temperature patterns, precipitation distribution, and extreme weather frequency. Understanding these systems enables weather prediction and climate change adaptation strategies. Technological advancement continues transforming society through innovations in various fields. Digital technology revolutionizes communication, information processing, and automation. Biotechnology applications include genetic engineering, medical treatments, and agricultural improvements. Materials science develops new substances with specific properties for various applications. Understanding technology principles enables informed decisions about its development and application while considering ethical implications. Mathematical relationships describe natural phenomena and enable scientific understanding. Quantitative analysis involves measurement, data collection, and statistical analysis. Graphs and equations represent relationships between variables, enabling prediction and modeling. Mathematical tools help solve complex problems and verify scientific theories. Understanding mathematical principles enables better scientific investigation and technological development. Human health involves physical, mental, and social well-being maintenance. The immune system protects against disease through various mechanisms including specific and non-specific responses. Nutrition principles guide proper diet selection for optimal health maintenance. Exercise benefits include improved cardiovascular health, strength, and mental well-being. Understanding health principles enables better personal wellness management and disease prevention.""" def get_certificate_title(self, base_title: str) -> str: """Get certificate title with difficulty level""" return f"{base_title} - {self.selected_level} Level" def generate_questions(self, text: str, num_questions: int) -> Tuple[bool, List[Question]]: """ Generate quiz questions using the QuizGenerator Returns (success, questions) tuple """ try: questions = self.quiz_generator.generate_questions(text, num_questions) self.current_questions = questions return True, questions except Exception as e: print(f"Error generating questions: {e}") return False, [] def calculate_score(self, answers: List[Optional[str]]) -> Tuple[float, bool, List[QuizFeedback]]: """ Calculate the quiz score and generate feedback Returns (score, passed, feedback) tuple """ if not answers or not self.current_questions: return 0, False, [] feedback = [] correct = 0 for question, answer in zip(self.current_questions, answers): if answer is None: feedback.append(QuizFeedback(False, None, question.options[question.correct_answer])) continue try: selected_index = question.options.index(answer) is_correct = selected_index == question.correct_answer if is_correct: correct += 1 feedback.append(QuizFeedback( is_correct, answer, question.options[question.correct_answer] )) except ValueError: feedback.append(QuizFeedback(False, answer, question.options[question.correct_answer])) score = (correct / len(self.current_questions)) * 100 return score, score >= 80, feedback def update_questions(self, text: str, num_questions: int) -> Tuple[gr.update, gr.update, List[gr.update], List[Question], gr.update]: """ Event handler for generating new questions """ if not text.strip(): return ( gr.update(value=""), gr.update(value="⚠️ Please enter some text content to generate questions."), *[gr.update(visible=False, choices=[]) for _ in range(5)], [], gr.update(selected=1) ) success, questions = self.generate_questions(text, num_questions) if not success or not questions: return ( gr.update(value=""), gr.update(value="❌ Failed to generate questions. Please try again."), *[gr.update(visible=False, choices=[]) for _ in range(5)], [], gr.update(selected=1) ) # Create question display questions_html = "# 📝 Assessment Questions\n\n" questions_html += "> Please select one answer for each question.\n\n" # Update radio buttons updates = [] for i, q in enumerate(questions): questions_html += f"### Question {i+1}\n{q.question}\n\n" updates.append(gr.update( visible=True, choices=q.options, value=None, label=f"Select your answer:" )) # Hide unused radio buttons for i in range(len(questions), 5): updates.append(gr.update(visible=False, choices=[])) return ( gr.update(value=questions_html), gr.update(value=""), *updates, questions, gr.update(selected=1) ) def submit_quiz(self, q1: Optional[str], q2: Optional[str], q3: Optional[str], q4: Optional[str], q5: Optional[str], questions: List[Question] ) -> Tuple[gr.update, List[gr.update], float, str, gr.update]: """ Event handler for quiz submission """ answers = [q1, q2, q3, q4, q5][:len(questions)] if not all(a is not None for a in answers): return ( gr.update(value="⚠️ Please answer all questions before submitting."), *[gr.update() for _ in range(5)], 0, "", gr.update(selected=1) ) score, passed, feedback = self.calculate_score(answers) # Create feedback HTML feedback_html = "# Assessment Results\n\n" for i, (q, f) in enumerate(zip(self.current_questions, feedback)): color = "green" if f.is_correct else "red" symbol = "✅" if f.is_correct else "❌" feedback_html += f""" ### Question {i+1} {q.question}
You passed the assessment with a score of {score:.1f}%
Your certificate has been generated.
Your score: {score:.1f}%
You need 80% or higher to pass and receive a certificate.
This assessment evaluates your understanding of Construction Safety.
Unanswered Questions: {', '.join(map(str, unanswered))}