import os from gradio_client import Client, file # --- 설정 --- # 로컬 Gradio 서버 주소 (app.py 실행 시 터미널에 표시되는 주소) SERVER_URL = "http://localhost:8006/" def run_chat_test(client): """일반 채팅 탭의 기능을 테스트합니다.""" print("\n--- 💬 일반 채팅 테스트 시작 ---") test_message = "안녕하세요! 오늘 날씨는 어떤가요?" chat_history = [] # 초기 대화 내역은 비어있음 print(f"보내는 메시지: '{test_message}'") # `respond` 함수 호출 (API 엔드포인트 인덱스: 0) # 입력: (메시지, 채팅 내역, 파일) # 출력: (비워진 텍스트 박스, 갱신된 채팅 내역) result = client.predict( test_message, chat_history, None, # 파일 없음 fn_index=0 ) # 갱신된 채팅 내역에서 마지막 응답(봇 메시지)을 추출 updated_history = result[1] bot_response = updated_history[-1]['content'] print("✅ 테스트 성공!") print(f"🤖 받은 응답: '{bot_response}'") def run_math_test(client): """수학 문제 해결 탭의 기능을 테스트합니다.""" print("\n--- 🧮 수학 문제 해결 테스트 시작 ---") test_problem = "두 개의 연속된 짝수의 합이 34일 때, 두 짝수는 무엇인가요?" print(f"보내는 문제: '{test_problem}'") # 수학 문제 해결 함수 호출 (API 엔드포인트 인덱스: 1) # 입력: (수학 문제, 파일) # 출력: (결과 텍스트) result = client.predict( test_problem, None, # 파일 없음 fn_index=1 ) print("✅ 테스트 성공!") print(f"🤖 받은 응답 (일부): '{result[:200]}...'") def run_file_test(client): """파일 업로드 기능을 테스트합니다.""" print("\n--- 📁 파일 업로드 채팅 테스트 시작 ---") # 테스트용 임시 텍스트 파일 생성 temp_file_path = "test_document.txt" with open(temp_file_path, "w", encoding="utf-8") as f: f.write("이 파일은 테스트를 위해 생성되었습니다.\n") f.write("파일의 핵심 내용은 '대한민국의 수도는 서울이다' 입니다.") print(f"업로드할 파일: '{temp_file_path}'") test_message = "업로드한 파일의 핵심 내용이 뭐야?" print(f"보내는 메시지: '{test_message}'") # `file()` 함수를 사용하여 파일을 서버에 업로드 가능한 형태로 변환 result = client.predict( test_message, [], # 채팅 내역 없음 file(temp_file_path), fn_index=0 ) # 임시 파일 삭제 os.remove(temp_file_path) bot_response = result[1][-1]['content'] print("✅ 테스트 성공!") print(f"🤖 받은 응답: '{bot_response}'") if __name__ == "__main__": print(f"Gradio 서버({SERVER_URL})에 연결을 시도합니다...") try: # 서버에 클라이언트로 연결 client = Client(SERVER_URL, verbose=False) print("✅ 서버 연결 성공!") # 테스트 실행 run_chat_test(client) run_math_test(client) # run_file_test(client) # 파일 테스트는 필요시 주석 해제하여 사용 except Exception as e: print(f"\n❌ 테스트 실패: 서버에 연결할 수 없거나 오류가 발생했습니다.") print("먼저 다른 터미널에서 'python app.py'를 실행했는지 확인해주세요.") print(f"오류 상세 정보: {e}")