Slicelayers commited on
Commit
83f12a5
·
verified ·
1 Parent(s): bcd697d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -34
app.py CHANGED
@@ -1,38 +1,37 @@
1
- import gradio as gr
2
  import requests
3
- import os
4
  import json
 
5
 
6
- NANO_BANANA_API_KEY = os.getenv("NANO_BANANA_API_KEY")
7
-
8
- # --- ここに関数追加 ---
9
- def segment_image(image):
10
- headers = {"Authorization": f"Bearer {NANO_BANANA_API_KEY}"}
11
- files = {"file": open(image, "rb")}
12
- response = requests.post(
13
- "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent", files=files, headers=headers
14
- )
15
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  print("Response status:", response.status_code)
17
- try:
18
- print(json.dumps(response.json(), indent=2, ensure_ascii=False))
19
- except Exception as e:
20
- print("JSON parse error:", e)
21
-
22
- if response.status_code == 200:
23
- data = response.json()
24
- if "images" in data:
25
- return [img["url"] for img in data["images"]]
26
- return ["処理に失敗しました"]
27
-
28
- # --- Gradio UI 部分 ---
29
- with gr.Blocks() as demo:
30
- gr.Markdown("## 🧠 Nano Banana 画像分割デモ")
31
-
32
- input_image = gr.Image(label="入力画像", type="filepath")
33
- run_btn = gr.Button("分割実行")
34
- output_gallery = gr.Gallery(label="分割結果", show_label=True, columns=2, height="auto")
35
-
36
- run_btn.click(segment_image, inputs=[input_image], outputs=[output_gallery])
37
-
38
- demo.launch()
 
 
1
  import requests
2
+ import base64
3
  import json
4
+ import os
5
 
6
+ API_KEY = os.getenv("NANO_NABABA_API_KEY") # Hugging Face Secrets に登録したキー
7
+ API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent"
8
+
9
+ def generate_completion(image_path, prompt="欠けている部分を補完してください。"):
10
+ with open(image_path, "rb") as f:
11
+ image_data = base64.b64encode(f.read()).decode("utf-8")
12
+
13
+ headers = {
14
+ "Content-Type": "application/json",
15
+ "x-goog-api-key": API_KEY
16
+ }
17
+
18
+ payload = {
19
+ "contents": [
20
+ {
21
+ "parts": [
22
+ {"text": prompt},
23
+ {
24
+ "inline_data": {
25
+ "mime_type": "image/png",
26
+ "data": image_data
27
+ }
28
+ }
29
+ ]
30
+ }
31
+ ]
32
+ }
33
+
34
+ response = requests.post(API_URL, headers=headers, json=payload)
35
  print("Response status:", response.status_code)
36
+ print(response.text)
37
+ return response