tomo2chin2 commited on
Commit
a48d7fb
·
verified ·
1 Parent(s): f3b1f86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,12 +1,12 @@
1
- import gradio as gr
2
  import base64
3
  from fastapi import FastAPI, File, UploadFile
4
  from fastapi.responses import JSONResponse
5
  from fastapi.staticfiles import StaticFiles
6
- from fastapi.middleware.cors import CORSMiddleware # 追加:CORS対応
7
  import uvicorn
8
  import os
9
- import glob # 追加:ファイル検索用
10
 
11
  # --- FastAPI アプリケーションの作成 ---
12
  app = FastAPI()
@@ -14,34 +14,42 @@ app = FastAPI()
14
  # --- CORS設定の追加 ---
15
  app.add_middleware(
16
  CORSMiddleware,
17
- allow_origins=["*"], # すべてのオリジンを許可
18
  allow_credentials=True,
19
- allow_methods=["*"], # すべてのメソッドを許可
20
- allow_headers=["*"], # すべてのヘッダーを許可
21
  )
22
 
23
  # --- 静的ファイルのサービング設定 ---
24
  # Gradioのディレクトリを探索してアセットを見つける
25
- gradio_dir = os.path.dirname(gradio.__file__)
 
 
 
 
26
 
27
  # 基本的な静的ファイルディレクトリをマウント
28
  static_dir = os.path.join(gradio_dir, "templates", "frontend", "static")
29
  if os.path.exists(static_dir):
 
30
  app.mount("/static", StaticFiles(directory=static_dir), name="static")
31
 
32
  # _appディレクトリを探す(新しいSvelteKitベースのフロントエンド用)
33
  app_dir = os.path.join(gradio_dir, "templates", "frontend", "_app")
34
  if os.path.exists(app_dir):
 
35
  app.mount("/_app", StaticFiles(directory=app_dir), name="_app")
36
 
37
  # assetsディレクトリを探す
38
  assets_dir = os.path.join(gradio_dir, "templates", "frontend", "assets")
39
  if os.path.exists(assets_dir):
 
40
  app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")
41
 
42
  # cdnディレクトリがあれば追加
43
  cdn_dir = os.path.join(gradio_dir, "templates", "cdn")
44
  if os.path.exists(cdn_dir):
 
45
  app.mount("/cdn", StaticFiles(directory=cdn_dir), name="cdn")
46
 
47
  # --- (既存の) Base64 エンコード関数 (async def に修正) ---
@@ -106,6 +114,4 @@ iface = gr.Interface(
106
  app = gr.mount_gradio_app(app, iface, path="/")
107
 
108
  if __name__ == "__main__":
109
- # Gradioのバージョンを表示
110
- print(f"Gradio version: {gr.__version__}")
111
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ import gradio as gr # ここでgradioをgrとしてインポート
2
  import base64
3
  from fastapi import FastAPI, File, UploadFile
4
  from fastapi.responses import JSONResponse
5
  from fastapi.staticfiles import StaticFiles
6
+ from fastapi.middleware.cors import CORSMiddleware
7
  import uvicorn
8
  import os
9
+ import glob
10
 
11
  # --- FastAPI アプリケーションの作成 ---
12
  app = FastAPI()
 
14
  # --- CORS設定の追加 ---
15
  app.add_middleware(
16
  CORSMiddleware,
17
+ allow_origins=["*"],
18
  allow_credentials=True,
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
  )
22
 
23
  # --- 静的ファイルのサービング設定 ---
24
  # Gradioのディレクトリを探索してアセットを見つける
25
+ gradio_dir = os.path.dirname(gr.__file__) # ここを修正: gradio → gr
26
+
27
+ # バージョン情報を表示
28
+ print(f"Gradio version: {gr.__version__}")
29
+ print(f"Gradio directory: {gradio_dir}")
30
 
31
  # 基本的な静的ファイルディレクトリをマウント
32
  static_dir = os.path.join(gradio_dir, "templates", "frontend", "static")
33
  if os.path.exists(static_dir):
34
+ print(f"Mounting static directory: {static_dir}")
35
  app.mount("/static", StaticFiles(directory=static_dir), name="static")
36
 
37
  # _appディレクトリを探す(新しいSvelteKitベースのフロントエンド用)
38
  app_dir = os.path.join(gradio_dir, "templates", "frontend", "_app")
39
  if os.path.exists(app_dir):
40
+ print(f"Mounting _app directory: {app_dir}")
41
  app.mount("/_app", StaticFiles(directory=app_dir), name="_app")
42
 
43
  # assetsディレクトリを探す
44
  assets_dir = os.path.join(gradio_dir, "templates", "frontend", "assets")
45
  if os.path.exists(assets_dir):
46
+ print(f"Mounting assets directory: {assets_dir}")
47
  app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")
48
 
49
  # cdnディレクトリがあれば追加
50
  cdn_dir = os.path.join(gradio_dir, "templates", "cdn")
51
  if os.path.exists(cdn_dir):
52
+ print(f"Mounting cdn directory: {cdn_dir}")
53
  app.mount("/cdn", StaticFiles(directory=cdn_dir), name="cdn")
54
 
55
  # --- (既存の) Base64 エンコード関数 (async def に修正) ---
 
114
  app = gr.mount_gradio_app(app, iface, path="/")
115
 
116
  if __name__ == "__main__":
 
 
117
  uvicorn.run(app, host="0.0.0.0", port=7860)