Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from langchain_groq import ChatGroq | |
| from langchain.chains import RetrievalQA | |
| from langchain.prompts import PromptTemplate | |
| from langchain.vectorstores import FAISS | |
| from langchain.embeddings import HuggingFaceEmbeddings | |
| # Groq API 키 (Hugging Face Secrets에서 가져옴) | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| # 전역 변수들 | |
| vectorstores = {} | |
| embeddings = None | |
| combined_vectorstore = None | |
| def debug_file_system(): | |
| """파일 시스템 상태를 자세히 확인하는 함수""" | |
| import os | |
| print("=" * 50) | |
| print("🔍 파일 시스템 디버깅 시작") | |
| print("=" * 50) | |
| # 현재 디렉토리 | |
| current_dir = os.getcwd() | |
| print(f"📂 현재 작업 디렉토리: {current_dir}") | |
| # 루트 디렉토리의 모든 항목 | |
| try: | |
| all_items = os.listdir('.') | |
| print(f"📋 루트 디렉토리 내용: {all_items}") | |
| # 각 항목의 타입 확인 | |
| for item in all_items: | |
| item_path = os.path.join('.', item) | |
| if os.path.isdir(item_path): | |
| print(f"📁 {item} (디렉토리)") | |
| try: | |
| sub_items = os.listdir(item_path) | |
| print(f" └── 내용: {sub_items}") | |
| # 벡터스토어 관련 파일 크기 확인 | |
| if item.startswith('vectorstore'): | |
| for sub_item in sub_items: | |
| sub_path = os.path.join(item_path, sub_item) | |
| if os.path.isfile(sub_path): | |
| size = os.path.getsize(sub_path) | |
| print(f" └── {sub_item}: {size:,} bytes") | |
| except Exception as e: | |
| print(f" └── 접근 불가: {e}") | |
| else: | |
| print(f"📄 {item} (파일)") | |
| except Exception as e: | |
| print(f"❌ 디렉토리 읽기 오류: {e}") | |
| # 환경 변수 확인 | |
| print(f"🔑 GROQ_API_KEY 설정됨: {'GROQ_API_KEY' in os.environ}") | |
| print("=" * 50) | |
| # 앱 시작 시 디버깅 실행 | |
| debug_file_system() | |
| def find_vectorstore_folders(): | |
| """현재 디렉토리에서 벡터스토어 폴더들을 찾는 함수""" | |
| current_dir = os.getcwd() | |
| print(f"현재 디렉토리: {current_dir}") | |
| # 모든 파일과 폴더 확인 | |
| try: | |
| all_items = os.listdir(current_dir) | |
| print(f"현재 디렉토리 내 모든 항목들: {all_items}") | |
| except Exception as e: | |
| print(f"디렉토리 읽기 오류: {e}") | |
| return [] | |
| # 예상되는 벡터스토어 폴더들 | |
| expected_folders = ['vectorstore1', 'vectorstore2', 'vectorstore3'] | |
| vectorstore_folders = [] | |
| for folder_name in expected_folders: | |
| folder_path = os.path.join(current_dir, folder_name) | |
| if os.path.exists(folder_path) and os.path.isdir(folder_path): | |
| try: | |
| folder_contents = os.listdir(folder_path) | |
| print(f"📁 {folder_name} 폴더 내용: {folder_contents}") | |
| # 필수 파일들 확인 | |
| required_files = ['index.faiss', 'index.pkl'] | |
| has_all_files = all(file in folder_contents for file in required_files) | |
| if has_all_files: | |
| # 파일 크기도 확인 | |
| faiss_path = os.path.join(folder_path, 'index.faiss') | |
| pkl_path = os.path.join(folder_path, 'index.pkl') | |
| faiss_size = os.path.getsize(faiss_path) if os.path.exists(faiss_path) else 0 | |
| pkl_size = os.path.getsize(pkl_path) if os.path.exists(pkl_path) else 0 | |
| if faiss_size > 0 and pkl_size > 0: | |
| vectorstore_folders.append(folder_name) | |
| print(f"✅ {folder_name} - 모든 필수 파일 존재 (FAISS: {faiss_size:,}bytes, PKL: {pkl_size:,}bytes)") | |
| else: | |
| print(f"❌ {folder_name} - 파일이 비어있음 (FAISS: {faiss_size}bytes, PKL: {pkl_size}bytes)") | |
| else: | |
| missing_files = [f for f in required_files if f not in folder_contents] | |
| print(f"❌ {folder_name} - 누락된 파일: {missing_files}") | |
| except Exception as e: | |
| print(f"❌ {folder_name} 폴더 확인 중 오류: {e}") | |
| else: | |
| print(f"❌ {folder_name} 폴더가 존재하지 않음") | |
| if not vectorstore_folders: | |
| print("❌ 사용 가능한 벡터스토어 폴더를 찾을 수 없습니다") | |
| print("💡 허깅페이스 스페이스에 벡터스토어 폴더들이 제대로 업로드되었는지 확인하세요") | |
| else: | |
| print(f"✅ 총 {len(vectorstore_folders)}개의 벡터스토어 폴더를 찾았습니다: {vectorstore_folders}") | |
| return vectorstore_folders | |
| def load_all_vectorstores(): | |
| """모든 벡터스토어를 로드하고 통합하는 함수""" | |
| global vectorstores, embeddings, combined_vectorstore | |
| print("🔄 임베딩 모델 로딩 중...") | |
| try: | |
| if not embeddings: | |
| embeddings = HuggingFaceEmbeddings( | |
| model_name="sentence-transformers/all-MiniLM-L6-v2" | |
| ) | |
| print("✅ 임베딩 모델 로드 완료") | |
| except Exception as e: | |
| print(f"❌ 임베딩 모델 로드 실패: {e}") | |
| return False | |
| # 벡터스토어 폴더들 찾기 | |
| folders = find_vectorstore_folders() | |
| if not folders: | |
| print("❌ 로드할 벡터스토어 폴더가 없습니다") | |
| return False | |
| loaded_vectorstores = [] | |
| for folder_name in folders: | |
| try: | |
| print(f"🔄 {folder_name} 로딩 중...") | |
| vectorstore = FAISS.load_local( | |
| f"./{folder_name}", | |
| embeddings, | |
| allow_dangerous_deserialization=True | |
| ) | |
| vectorstores[folder_name] = vectorstore | |
| loaded_vectorstores.append(vectorstore) | |
| # 벡터스토어 정보 출력 | |
| doc_count = vectorstore.index.ntotal | |
| print(f"✅ {folder_name} 로드 완료 (문서 수: {doc_count})") | |
| except Exception as e: | |
| print(f"❌ {folder_name} 로드 실패: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| # 벡터스토어들을 통합 | |
| if loaded_vectorstores: | |
| print("🔄 벡터스토어 통합 중...") | |
| try: | |
| combined_vectorstore = loaded_vectorstores[0] | |
| total_docs = combined_vectorstore.index.ntotal | |
| for i, vs in enumerate(loaded_vectorstores[1:], 1): | |
| combined_vectorstore.merge_from(vs) | |
| total_docs += vs.index.ntotal | |
| print(f"✅ 벡터스토어 {i+1} 통합 완료") | |
| print(f"🎉 총 {len(loaded_vectorstores)}개의 벡터스토어가 통합되었습니다 (총 문서 수: {total_docs})") | |
| return True | |
| except Exception as e: | |
| print(f"❌ 벡터스토어 통합 실패: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| else: | |
| print("❌ 로드된 벡터스토어가 없습니다") | |
| return False | |
| # 질문 리스트 | |
| suggested_questions = [ | |
| '교원 신규 임용은 어떻게 하나요?', | |
| '교원 연구년 기간은 어떻게 되나요?', | |
| '조교 신규 임용 기준은 무엇인가요?', | |
| '교직원의 평일 근무시간은 어떻게 되나요?', | |
| '직원 신규 임용 원칙은 무엇인가요?', | |
| '직원 임용시 가산점이 있나요?', | |
| '교원 업적의 심사 내용은 무엇인가요?', | |
| '외국인 교원의 임기는 어떻게 되나요?', | |
| '외국인 교원의 면직 기준은 무엇인가요?', | |
| '기간제 계약직의 임기는 얼마정도인가요?', | |
| '등록금 납부 방법은 무엇인가요?', | |
| '교직 이수는 언제 신청이 가능한가요?', | |
| '해외교류유학 지원자격은 어떻게 되나요?', | |
| '만족도 조사 실행 대상은 누구인가요?', | |
| '마이크로디그리의 유형은 무엇이 있나요?', | |
| '장학금 관리 기관은 어디인가요?', | |
| '학생 단체는 어떻게 등록하나요?', | |
| '학생 설치물 중 금지된 설치물이 있나요?', | |
| '비교과 교육과정의 종류는 무엇이 있나요?', | |
| '안전사고예방계획은 어디에 제출해야 하나요?' | |
| ] | |
| # 프롬프트 템플릿 | |
| prompt_template = """당신은 한남대학교 규정집 도우미입니다. | |
| 반드시 한국어로만 답변해주세요. 영어나 다른 언어는 절대 사용하지 마세요. | |
| 주어진 문서 내용을 바탕으로 질문에 대해 정확하고 친절하게 한국어로 답변해주세요. | |
| 참고 문서: | |
| {context} | |
| 질문: {question} | |
| 답변 지침: | |
| - 이용자를 반기는 인사로 시작하세요 | |
| - 반드시 한국어로만 답변하세요 | |
| - 정중하고 친근한 말투를 사용하세요 | |
| - 구체적이고 도움이 되는 정보를 제공하세요 | |
| - 문서에서 답을 찾을 수 없으면 "죄송하지만 해당 정보를 규정집에서 찾을 수 없습니다"라고 답변하세요 | |
| 한국어 답변:""" | |
| prompt = PromptTemplate( | |
| template=prompt_template, | |
| input_variables=["context", "question"] | |
| ) | |
| def respond_with_groq(question, selected_q, model): | |
| """질문에 대한 답변을 생성하는 함수""" | |
| # 선택된 질문이 있으면 그것을 사용 | |
| if selected_q != "직접 입력": | |
| question = selected_q | |
| if not question.strip(): | |
| return "질문을 입력해주세요." | |
| if not GROQ_API_KEY: | |
| return "❌ API 키가 설정되지 않았습니다. 관리자에게 문의하세요." | |
| # 통합된 벡터스토어가 로드되지 않은 경우 | |
| if not combined_vectorstore: | |
| return "❌ 사용 가능한 벡터스토어가 없습니다. 벡터스토어 파일들이 제대로 업로드되었는지 확인하세요." | |
| try: | |
| print(f"✅ 통합된 벡터스토어를 사용하여 검색 중... (질문: {question})") | |
| # LLM 설정 | |
| llm = ChatGroq( | |
| groq_api_key=GROQ_API_KEY, | |
| model_name=model, | |
| temperature=0.1, | |
| max_tokens=1000 | |
| ) | |
| # QA 체인 생성 | |
| qa_chain = RetrievalQA.from_chain_type( | |
| llm=llm, | |
| chain_type="stuff", | |
| retriever=combined_vectorstore.as_retriever(search_kwargs={"k": 5}), | |
| chain_type_kwargs={"prompt": prompt}, | |
| return_source_documents=True | |
| ) | |
| # 답변 생성 | |
| result = qa_chain({"query": question}) | |
| print(f"✅ 답변 생성 완료") | |
| return result['result'] | |
| except Exception as e: | |
| import traceback | |
| error_details = traceback.format_exc() | |
| print(f"❌ 상세 오류 정보:\n{error_details}") | |
| return f"❌ 오류가 발생했습니다: {str(e)}" | |
| def update_question(selected): | |
| """드롭다운 선택 시 질문을 업데이트하는 함수""" | |
| if selected != "직접 입력": | |
| return selected | |
| return "" | |
| def get_system_status(): | |
| """시스템 상태를 반환하는 함수""" | |
| status = [] | |
| status.append(f"🔑 API 키: {'✅ 설정됨' if GROQ_API_KEY else '❌ 없음'}") | |
| status.append(f"🤖 임베딩: {'✅ 로드됨' if embeddings else '❌ 없음'}") | |
| status.append(f"📚 벡터스토어: {'✅ 로드됨' if combined_vectorstore else '❌ 없음'}") | |
| if combined_vectorstore: | |
| doc_count = combined_vectorstore.index.ntotal | |
| status.append(f"📄 총 문서 수: {doc_count:,}개") | |
| return "\n".join(status) | |
| # 앱 시작시 벡터스토어들 로드 | |
| print("🚀 애플리케이션 시작 - 벡터스토어 로딩 중...") | |
| vectorstores_loaded = load_all_vectorstores() | |
| if vectorstores_loaded: | |
| print("🎉 모든 벡터스토어가 성공적으로 로드되었습니다!") | |
| else: | |
| print("❌ 벡터스토어 로드에 실패했습니다. 파일 업로드를 확인하세요.") | |
| # Gradio 인터페이스 생성 | |
| with gr.Blocks(title="한남대학교 Q&A") as interface: | |
| gr.HTML(""" | |
| <div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;"> | |
| <h1>🏫 한남대학교 규정집 Q&A</h1> | |
| <p>한남대학교 규정집에 대한 질문에 답변해드립니다.</p> | |
| </div> | |
| """) | |
| # 시스템 상태 표시 | |
| with gr.Row(): | |
| system_status = gr.Textbox( | |
| label="🔧 시스템 상태", | |
| value=get_system_status(), | |
| lines=4, | |
| interactive=False | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| question_dropdown = gr.Dropdown( | |
| choices=["직접 입력"] + suggested_questions, | |
| label="💡 자주 묻는 질문", | |
| value="직접 입력" | |
| ) | |
| question_input = gr.Textbox( | |
| label="❓ 질문을 입력하세요", | |
| placeholder="예: 졸업 요건은 무엇인가요?", | |
| lines=3 | |
| ) | |
| submit_btn = gr.Button("답변 받기", variant="primary", size="lg") | |
| model_choice = gr.Radio( | |
| choices=["llama3-70b-8192", "llama3-8b-8192"], | |
| label="🤖 AI 모델 선택", | |
| value="llama3-70b-8192" | |
| ) | |
| with gr.Column(scale=2): | |
| output = gr.Textbox( | |
| label="💬 답변", | |
| lines=15, | |
| max_lines=20, | |
| show_copy_button=True | |
| ) | |
| # 이벤트 연결 | |
| submit_btn.click( | |
| fn=respond_with_groq, | |
| inputs=[question_input, question_dropdown, model_choice], | |
| outputs=output | |
| ) | |
| question_dropdown.change( | |
| fn=update_question, | |
| inputs=question_dropdown, | |
| outputs=question_input | |
| ) | |
| # 앱 실행 | |
| if __name__ == "__main__": | |
| interface.launch() |