tomo2chin2 commited on
Commit
f30d0bf
·
verified ·
1 Parent(s): 50e9955

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -3,25 +3,46 @@ import base64
3
  from fastapi import FastAPI, File, UploadFile
4
  from fastapi.responses import JSONResponse
5
  from fastapi.staticfiles import StaticFiles
 
6
  import uvicorn
7
  import os
 
8
 
9
  # --- FastAPI アプリケーションの作成 ---
10
  app = FastAPI()
11
 
 
 
 
 
 
 
 
 
 
12
  # --- 静的ファイルのサービング設定 ---
13
- # Gradioの静的ファイルディレクトリをマウント
14
- import gradio
15
  gradio_dir = os.path.dirname(gradio.__file__)
16
- static_dir = os.path.join(gradio_dir, "templates", "frontend", "static")
17
 
18
- # 静的ファイルをマウント(存在する場合のみ)
 
19
  if os.path.exists(static_dir):
20
  app.mount("/static", StaticFiles(directory=static_dir), name="static")
21
 
22
- # プロジェクト内に static ディレクトリがある場合も追加でマウント
23
- if os.path.exists("static"):
24
- app.mount("/custom-static", StaticFiles(directory="static"), name="custom-static")
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # --- (既存の) Base64 エンコード関数 (async def に修正) ---
27
  async def encode_to_base64(file_obj):
@@ -70,17 +91,21 @@ output_text = gr.Textbox(
70
  interactive=True
71
  )
72
 
 
73
  iface = gr.Interface(
74
  fn=encode_to_base64,
75
  inputs=input_file,
76
  outputs=output_text,
77
  title="ファイル ⇨ Base64 エンコーダー (Gradio & API)",
78
  description="画像またはPDFファイルをアップロードしてBase64エンコード。\nAPIエンドポイント: `/api/encode_base64` (POST)",
79
- allow_flagging='never'
 
80
  )
81
 
82
  # --- アプリケーションの起動 ---
83
  app = gr.mount_gradio_app(app, iface, path="/")
84
 
85
  if __name__ == "__main__":
 
 
86
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
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()
13
 
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 に修正) ---
48
  async def encode_to_base64(file_obj):
 
91
  interactive=True
92
  )
93
 
94
+ # Gradioインターフェースの設定
95
  iface = gr.Interface(
96
  fn=encode_to_base64,
97
  inputs=input_file,
98
  outputs=output_text,
99
  title="ファイル ⇨ Base64 エンコーダー (Gradio & API)",
100
  description="画像またはPDFファイルをアップロードしてBase64エンコード。\nAPIエンドポイント: `/api/encode_base64` (POST)",
101
+ allow_flagging='never',
102
+ theme=gr.themes.Base() # 明示的にテーマを指定
103
  )
104
 
105
  # --- アプリケーションの起動 ---
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)