import gradio as gr import json from pathlib import Path import re # 현재 디렉토리 설정 current_dir = Path(__file__).parent def create_quiz_app(): """Gradio 앱 생성""" # simple_quiz.html 파일 읽기 (테스트용 전체 문서) html_file = current_dir / "simple_quiz.html" if html_file.exists(): with open(html_file, 'r', encoding='utf-8') as f: full_html = f.read() else: full_html = """ 퀴즈

퀴즈 파일을 찾을 수 없습니다.

""" # questions.json 파일 읽기 (webquiz/ 하위 우선, 없으면 루트 fallback) questions_file = current_dir / "webquiz" / "questions.json" if not questions_file.exists(): fallback = current_dir / "questions.json" if fallback.exists(): questions_file = fallback if questions_file.exists(): with open(questions_file, 'r', encoding='utf-8') as f: questions_data = json.load(f) # JavaScript에서 questions 데이터를 직접 사용할 수 있도록 수정 questions_js = json.dumps(questions_data, ensure_ascii=False) # 기존 문서 내에 샘플 데이터가 있다면 교체, 없으면 주입 if 'const questions = [' in full_html: full_html = full_html.replace( 'const questions = [', f'const questions = {questions_js}; const _old_questions = [' ) else: # 바로 뒤에 questions 변수를 주입 full_html = re.sub( r"]*>", lambda m: f"{m.group(0)}\n", full_html, flags=re.IGNORECASE ) # srcdoc에 안전하게 삽입하기 위해 작은따옴표를 HTML 엔티티로 치환 srcdoc_html = full_html.replace("'", "'") # iframe(srcdoc)로 전체 문서를 로드하여 스크립트가 확실히 실행되도록 함 iframe_html = f"""
""" with gr.Blocks( title="인적자원관리 퀴즈", theme=gr.themes.Soft(), css=""" .gradio-container { max-width: none !important; padding: 0 !important; } .main { padding: 0 !important; } .block { border: none !important; box-shadow: none !important; margin: 0 !important; } /* Responsive iframe sizing */ .iframe-wrap { width: 100%; height: 100vh; } .iframe { width: 100%; height: 100%; border: none; } @media (max-width: 480px) { .iframe-wrap { height: 92vh; } .iframe { height: 100%; } } """ ) as app: # 퀴즈 인터페이스를 HTML로 직접 렌더링 (iframe 사용) gr.HTML( value=iframe_html, elem_id="quiz_interface" ) return app # Gradio 앱 생성 및 실행 if __name__ == "__main__": app = create_quiz_app() app.launch( server_name="0.0.0.0", server_port=7860, share=False, show_error=True )