Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,13 @@ 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 エンコード関数 ---
|
| 11 |
-
def encode_to_base64(file_obj):
|
| 12 |
"""
|
| 13 |
gr.FileまたはUploadFileから受け取ったファイルオブジェクトを処理し、
|
| 14 |
Base64エンコードされた文字列を返す関数。
|
|
@@ -18,7 +18,7 @@ def encode_to_base64(file_obj):
|
|
| 18 |
|
| 19 |
try:
|
| 20 |
if isinstance(file_obj, UploadFile): # FastAPIのUploadFileの場合
|
| 21 |
-
file_bytes = await file_obj.read()
|
| 22 |
else: # GradioのFileの場合 (file_obj.name はファイルパス)
|
| 23 |
file_path = file_obj.name
|
| 24 |
with open(file_path, 'rb') as f:
|
|
@@ -38,10 +38,10 @@ async def api_encode_base64(file: UploadFile = File(...)):
|
|
| 38 |
"""
|
| 39 |
APIエンドポイント: ファイルを受け取りBase64エンコードしてJSONで返す
|
| 40 |
"""
|
| 41 |
-
base64_string = await encode_to_base64(file)
|
| 42 |
-
if "エラー" in base64_string:
|
| 43 |
-
return JSONResponse(content={"error": base64_string}, status_code=400)
|
| 44 |
-
return {"base64_string": base64_string}
|
| 45 |
|
| 46 |
# --- Gradio インターフェース (変更なし) ---
|
| 47 |
input_file = gr.File(
|
|
@@ -64,8 +64,7 @@ iface = gr.Interface(
|
|
| 64 |
)
|
| 65 |
|
| 66 |
# --- アプリケーションの起動 ---
|
| 67 |
-
|
| 68 |
-
app = gr.mount_gradio_app(app, iface, path="/gradio") # GradioアプリをFastAPIにマウント
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 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): # ここを修正: def → async def
|
| 12 |
"""
|
| 13 |
gr.FileまたはUploadFileから受け取ったファイルオブジェクトを処理し、
|
| 14 |
Base64エンコードされた文字列を返す関数。
|
|
|
|
| 18 |
|
| 19 |
try:
|
| 20 |
if isinstance(file_obj, UploadFile): # FastAPIのUploadFileの場合
|
| 21 |
+
file_bytes = await file_obj.read()
|
| 22 |
else: # GradioのFileの場合 (file_obj.name はファイルパス)
|
| 23 |
file_path = file_obj.name
|
| 24 |
with open(file_path, 'rb') as f:
|
|
|
|
| 38 |
"""
|
| 39 |
APIエンドポイント: ファイルを受け取りBase64エンコードしてJSONで返す
|
| 40 |
"""
|
| 41 |
+
base64_string = await encode_to_base64(file)
|
| 42 |
+
if "エラー" in base64_string:
|
| 43 |
+
return JSONResponse(content={"error": base64_string}, status_code=400)
|
| 44 |
+
return {"base64_string": base64_string}
|
| 45 |
|
| 46 |
# --- Gradio インターフェース (変更なし) ---
|
| 47 |
input_file = gr.File(
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
# --- アプリケーションの起動 ---
|
| 67 |
+
app = gr.mount_gradio_app(app, iface, path="/gradio")
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|