tomo2chin2 commited on
Commit
2bb7232
·
verified ·
1 Parent(s): b227589

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
3
+
4
+ def file_to_base64(file_path):
5
+ """
6
+ アップロードされたファイルのパスを受け取り、
7
+ バイナリモードでファイルを読み込んで Base64 エンコードした文字列を返します。
8
+ """
9
+ try:
10
+ with open(file_path, "rb") as f:
11
+ data = f.read()
12
+ encoded_str = base64.b64encode(data).decode("utf-8")
13
+ return encoded_str
14
+ except Exception as e:
15
+ return f"ファイルの変換中にエラーが発生しました: {e}"
16
+
17
+ # Gradio インターフェースの定義
18
+ demo = gr.Interface(
19
+ fn=file_to_base64,
20
+ inputs=gr.File(
21
+ label="画像またはPDFファイルをアップロード",
22
+ type="file",
23
+ file_types=["jpg", "jpeg", "png", "pdf"]
24
+ ),
25
+ outputs=gr.Textbox(label="Base64 出力"),
26
+ title="ファイルから Base64 変換アプリ",
27
+ description="画像やPDFファイルをアップロードすると、その内容をBase64エンコードして表示します。"
28
+ )
29
+
30
+ # Hugging Face Spaces 用: Spaces は「app」という変数を探すため、これを定義します。
31
+ app = demo
32
+
33
+ if __name__ == "__main__":
34
+ demo.launch()