Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import warnings
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
|
| 4 |
-
#
|
|
|
|
| 5 |
load_dotenv()
|
|
|
|
| 6 |
|
| 7 |
-
# Import handlers and UI creator from modules
|
| 8 |
from prediction import single_prediction
|
| 9 |
from chatbot import process_chatbot_query_with_llm
|
| 10 |
from ui import create_ui
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if __name__ == "__main__":
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Create the Gradio UI by passing the handlers to the UI generator
|
| 27 |
-
demo = create_ui(
|
| 28 |
-
prediction_handler=single_prediction,
|
| 29 |
-
chatbot_handler=process_chatbot_query_with_llm
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
# Launch the application
|
| 33 |
-
# share=True creates a public link
|
| 34 |
-
# server_name="0.0.0.0" allows external connections
|
| 35 |
-
# server_port=7860 is the default Hugging Face Spaces port
|
| 36 |
-
demo.launch(
|
| 37 |
-
share=False, # Set to True for public sharing
|
| 38 |
-
server_name="0.0.0.0", # For Hugging Face Spaces
|
| 39 |
-
server_port=7860 # Default HF Spaces port
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
# Alternative launch configurations:
|
| 43 |
-
# For local development:
|
| 44 |
-
# demo.launch(share=False)
|
| 45 |
-
|
| 46 |
-
# For Hugging Face Spaces:
|
| 47 |
-
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 48 |
-
|
| 49 |
-
# For public sharing with ngrok:
|
| 50 |
-
# demo.launch(share=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import uvicorn
|
| 5 |
+
import os
|
| 6 |
import warnings
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
+
# --- 1. 환경 변수 및 모듈 로드 ---
|
| 10 |
+
# 이 부분은 기존 코드와 동일합니다.
|
| 11 |
load_dotenv()
|
| 12 |
+
warnings.filterwarnings('ignore')
|
| 13 |
|
|
|
|
| 14 |
from prediction import single_prediction
|
| 15 |
from chatbot import process_chatbot_query_with_llm
|
| 16 |
from ui import create_ui
|
| 17 |
|
| 18 |
+
|
| 19 |
+
# --- 2. FastAPI 애플리케이션 생성 및 설정 ---
|
| 20 |
+
|
| 21 |
+
# FastAPI 앱을 생성합니다. 이제부터 이 앱이 전체 서버를 관리합니다.
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
# CORS (Cross-Origin Resource Sharing) 미들웨어를 추가합니다.
|
| 25 |
+
# 이 부분이 WebSocket 403 Forbidden 오류를 해결하는 핵심입니다.
|
| 26 |
+
# 외부(로컬 PC, 다른 웹사이트 등)에서의 모든 종류의 접속을 허용합니다.
|
| 27 |
+
app.add_middleware(
|
| 28 |
+
CORSMiddleware,
|
| 29 |
+
allow_origins=["*"], # 모든 출처에서의 접속을 허용합니다.
|
| 30 |
+
allow_credentials=True, # 인증 정보 (쿠키 등)를 허용합니다.
|
| 31 |
+
allow_methods=["*"], # 모든 HTTP 메소드 (GET, POST 등)를 허용합니다.
|
| 32 |
+
allow_headers=["*"], # 모든 HTTP 헤더를 허용합니다.
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# --- 3. Gradio UI 생성 및 FastAPI에 통합 ---
|
| 37 |
+
|
| 38 |
+
# 기존과 동일하게 UI 생성 함수를 호출하여 Gradio 데모 객체를 만듭니다.
|
| 39 |
+
gradio_demo = create_ui(
|
| 40 |
+
prediction_handler=single_prediction,
|
| 41 |
+
chatbot_handler=process_chatbot_query_with_llm
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# FastAPI 앱에 Gradio 데모를 탑재(mount)합니다.
|
| 45 |
+
# 이 함수는 WebSocket 경로를 포함한 모든 것을 올바르게 처리하는 표준 방식입니다.
|
| 46 |
+
app = gr.mount_gradio_app(
|
| 47 |
+
app, # 마운트할 FastAPI 앱
|
| 48 |
+
gradio_demo, # 마운트할 Gradio 데모
|
| 49 |
+
path="/gradio_api" # UI와 API가 서비스될 기본 경로
|
| 50 |
)
|
| 51 |
|
| 52 |
+
|
| 53 |
+
# --- 4. (선택사항) 서버 상태 확인용 루트 경로 ---
|
| 54 |
+
# 스페이스 주소로 바로 접속했을 때 서버가 살아있는지 확인할 수 있습니다.
|
| 55 |
+
@app.get("/")
|
| 56 |
+
def read_root():
|
| 57 |
+
return {
|
| 58 |
+
"status": "ok",
|
| 59 |
+
"message": "FastAPI server is running.",
|
| 60 |
+
"ui_path": "/gradio_api"
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# --- 5. (참고) 로컬 개발 환경에서 실행하기 위한 코드 ---
|
| 65 |
+
# Hugging Face Spaces에서는 이 부분이 직접 사용되지 않을 수 있지만,
|
| 66 |
+
# 로컬에서 'python app.py'로 테스트할 때 유용합니다.
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
print("로컬 개발 서버를 시작합니다. http://127.0.0.1:7860/gradio_api 에서 확인하세요.")
|
| 69 |
+
uvicorn.run(app, host="127.0.0.1", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|