Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,29 @@ import gradio as gr
|
|
| 2 |
import base64
|
| 3 |
from fastapi import FastAPI, File, UploadFile
|
| 4 |
from fastapi.responses import JSONResponse
|
|
|
|
| 5 |
import uvicorn
|
|
|
|
| 6 |
|
| 7 |
# --- FastAPI アプリケーションの作成 ---
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# --- (既存の) Base64 エンコード関数 (async def に修正) ---
|
| 11 |
-
async def encode_to_base64(file_obj):
|
| 12 |
"""
|
| 13 |
gr.FileまたはUploadFileから受け取ったファイルオブジェクトを処理し、
|
| 14 |
Base64エンコードされた文字列を返す関数。
|
|
@@ -43,7 +59,7 @@ async def api_encode_base64(file: UploadFile = File(...)):
|
|
| 43 |
return JSONResponse(content={"error": base64_string}, status_code=400)
|
| 44 |
return {"base64_string": base64_string}
|
| 45 |
|
| 46 |
-
# --- Gradio インターフェース
|
| 47 |
input_file = gr.File(
|
| 48 |
label="画像またはPDFファイルを入力",
|
| 49 |
file_types=['image', '.pdf']
|
|
@@ -67,4 +83,4 @@ iface = gr.Interface(
|
|
| 67 |
app = gr.mount_gradio_app(app, iface, path="/")
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
uvicorn.run(app, host="0.0.0.0",port=7860)
|
|
|
|
| 2 |
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):
|
| 28 |
"""
|
| 29 |
gr.FileまたはUploadFileから受け取ったファイルオブジェクトを処理し、
|
| 30 |
Base64エンコードされた文字列を返す関数。
|
|
|
|
| 59 |
return JSONResponse(content={"error": base64_string}, status_code=400)
|
| 60 |
return {"base64_string": base64_string}
|
| 61 |
|
| 62 |
+
# --- Gradio インターフェース ---
|
| 63 |
input_file = gr.File(
|
| 64 |
label="画像またはPDFファイルを入力",
|
| 65 |
file_types=['image', '.pdf']
|
|
|
|
| 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)
|