soiz commited on
Commit
e440439
·
verified ·
1 Parent(s): c682aab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -3
app.py CHANGED
@@ -1,8 +1,38 @@
1
  import os
 
2
  import gradio as gr
 
 
3
 
4
- # 環境変数からAPIキーを取得
5
  api_token = os.getenv("HF_API_KEY")
 
6
 
7
- # Hugging FaceモデルをAPIトークンで読み込む
8
- gr.load("models/often/removebg", api_key=api_token).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import requests
3
  import gradio as gr
4
+ from PIL import Image
5
+ from io import BytesIO
6
 
7
+ # Hugging Face APIトークンを環境変数から取得
8
  api_token = os.getenv("HF_API_KEY")
9
+ headers = {"Authorization": f"Bearer {api_token}"}
10
 
11
+ # 背景を削除する関数
12
+ def remove_background(image):
13
+ # Hugging Face APIエンドポイントにリクエストを送信
14
+ image_bytes = BytesIO()
15
+ image.save(image_bytes, format="PNG")
16
+ response = requests.post(
17
+ "https://api-inference.huggingface.co/models/often/removebg",
18
+ headers=headers,
19
+ files={"file": image_bytes.getvalue()}
20
+ )
21
+
22
+ if response.status_code == 200:
23
+ # 応答を画像に変換して返す
24
+ result_image = Image.open(BytesIO(response.content))
25
+ return result_image
26
+ else:
27
+ # エラー時の処理
28
+ return f"Error: {response.status_code}, {response.text}"
29
+
30
+ # Gradioインターフェースを作成
31
+ interface = gr.Interface(
32
+ fn=remove_background,
33
+ inputs=gr.Image(type="pil"),
34
+ outputs="image",
35
+ title="Background Remover"
36
+ )
37
+
38
+ interface.launch()