Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,40 +9,25 @@ import os
|
|
| 9 |
from google import genai
|
| 10 |
from google.genai import types
|
| 11 |
|
| 12 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
|
| 14 |
def infer(
|
| 15 |
google_api_key,
|
| 16 |
prompt,
|
| 17 |
-
seed,
|
| 18 |
-
randomize_seed,
|
| 19 |
progress=gr.Progress(track_tqdm=True),
|
| 20 |
):
|
| 21 |
# Google APIキーが入力されているかチェック
|
| 22 |
if not google_api_key:
|
| 23 |
raise gr.Error("Google API Key is required. Please enter your key.")
|
| 24 |
-
|
| 25 |
-
# APIキーを設定
|
| 26 |
-
# try:
|
| 27 |
-
# genai.configure(api_key=google_api_key)
|
| 28 |
-
# except Exception as e:
|
| 29 |
-
# raise gr.Error(f"Failed to configure Google API: {e}")
|
| 30 |
-
|
| 31 |
-
if randomize_seed:
|
| 32 |
-
seed = random.randint(0, MAX_SEED)
|
| 33 |
-
|
| 34 |
-
# --- ここから画像生成コードを組み込み ---
|
| 35 |
-
try:
|
| 36 |
|
|
|
|
|
|
|
| 37 |
client = genai.Client(api_key=google_api_key)
|
| 38 |
-
# シード値をリクエストに含める(APIが対応している場合)
|
| 39 |
-
# 注: generate_contentが直接seedをサポートしていない場合、プロンプトに含めるなどの工夫が必要になることがあります。
|
| 40 |
-
# ここでは、APIがseedを直接受け取らないため、プロンプトに情報を追加する例を示します。
|
| 41 |
-
# 実際の生成結果のランダム性はAPIの実装に依存します。
|
| 42 |
-
full_prompt = f"{prompt} (Seed: {seed})"
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
response = client.models.generate_content(
|
| 45 |
-
model="gemini-
|
| 46 |
contents=[full_prompt],
|
| 47 |
)
|
| 48 |
|
|
@@ -52,12 +37,10 @@ def infer(
|
|
| 52 |
if part.inline_data is not None:
|
| 53 |
image_part = part
|
| 54 |
break
|
| 55 |
-
|
| 56 |
if image_part:
|
| 57 |
image = Image.open(BytesIO(image_part.inline_data.data))
|
| 58 |
-
|
| 59 |
-
# image.save(f"generated_image_seed_{seed}.png")
|
| 60 |
-
return image, seed
|
| 61 |
else:
|
| 62 |
# テキスト部分があればエラーとして表示
|
| 63 |
text_response = ""
|
|
@@ -68,7 +51,6 @@ def infer(
|
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
raise gr.Error(f"An error occurred during image generation: {e}")
|
| 71 |
-
# --- ここまで ---
|
| 72 |
|
| 73 |
examples = [
|
| 74 |
"Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme",
|
|
@@ -86,14 +68,14 @@ css = """
|
|
| 86 |
with gr.Blocks(css=css) as demo:
|
| 87 |
with gr.Column(elem_id="col-container"):
|
| 88 |
gr.Markdown(" # Text-to-Image with Google Gemini")
|
| 89 |
-
|
| 90 |
google_api_key = gr.Textbox(
|
| 91 |
label="Google API Key",
|
| 92 |
placeholder="Enter your Google API Key here...",
|
| 93 |
type="password",
|
| 94 |
show_label=True,
|
| 95 |
)
|
| 96 |
-
|
| 97 |
with gr.Row():
|
| 98 |
prompt = gr.Text(
|
| 99 |
label="Prompt",
|
|
@@ -103,20 +85,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 103 |
container=False,
|
| 104 |
)
|
| 105 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 106 |
-
|
| 107 |
result = gr.Image(label="Result", show_label=False)
|
| 108 |
-
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
seed = gr.Slider(
|
| 112 |
-
label="Seed",
|
| 113 |
-
minimum=0,
|
| 114 |
-
maximum=MAX_SEED,
|
| 115 |
-
step=1,
|
| 116 |
-
value=0,
|
| 117 |
-
)
|
| 118 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 119 |
-
|
| 120 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 121 |
|
| 122 |
gr.on(
|
|
@@ -125,10 +98,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 125 |
inputs=[
|
| 126 |
google_api_key,
|
| 127 |
prompt,
|
| 128 |
-
seed,
|
| 129 |
-
randomize_seed,
|
| 130 |
],
|
| 131 |
-
outputs=[result
|
| 132 |
)
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
|
|
|
| 9 |
from google import genai
|
| 10 |
from google.genai import types
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
def infer(
|
| 14 |
google_api_key,
|
| 15 |
prompt,
|
|
|
|
|
|
|
| 16 |
progress=gr.Progress(track_tqdm=True),
|
| 17 |
):
|
| 18 |
# Google APIキーが入力されているかチェック
|
| 19 |
if not google_api_key:
|
| 20 |
raise gr.Error("Google API Key is required. Please enter your key.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
# Clientの初期化時にAPIキーを渡す
|
| 24 |
client = genai.Client(api_key=google_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# プロンプトにシード情報を含めることで、再現性のヒントとする
|
| 27 |
+
full_prompt = f"{prompt}"
|
| 28 |
+
|
| 29 |
response = client.models.generate_content(
|
| 30 |
+
model="gemini-2.5-flash-image-preview",
|
| 31 |
contents=[full_prompt],
|
| 32 |
)
|
| 33 |
|
|
|
|
| 37 |
if part.inline_data is not None:
|
| 38 |
image_part = part
|
| 39 |
break
|
| 40 |
+
|
| 41 |
if image_part:
|
| 42 |
image = Image.open(BytesIO(image_part.inline_data.data))
|
| 43 |
+
return image
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
# テキスト部分があればエラーとして表示
|
| 46 |
text_response = ""
|
|
|
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
raise gr.Error(f"An error occurred during image generation: {e}")
|
|
|
|
| 54 |
|
| 55 |
examples = [
|
| 56 |
"Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme",
|
|
|
|
| 68 |
with gr.Blocks(css=css) as demo:
|
| 69 |
with gr.Column(elem_id="col-container"):
|
| 70 |
gr.Markdown(" # Text-to-Image with Google Gemini")
|
| 71 |
+
|
| 72 |
google_api_key = gr.Textbox(
|
| 73 |
label="Google API Key",
|
| 74 |
placeholder="Enter your Google API Key here...",
|
| 75 |
type="password",
|
| 76 |
show_label=True,
|
| 77 |
)
|
| 78 |
+
|
| 79 |
with gr.Row():
|
| 80 |
prompt = gr.Text(
|
| 81 |
label="Prompt",
|
|
|
|
| 85 |
container=False,
|
| 86 |
)
|
| 87 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 88 |
+
|
| 89 |
result = gr.Image(label="Result", show_label=False)
|
| 90 |
+
|
| 91 |
+
# --- Advanced Settingsを削除 ---
|
| 92 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 94 |
|
| 95 |
gr.on(
|
|
|
|
| 98 |
inputs=[
|
| 99 |
google_api_key,
|
| 100 |
prompt,
|
|
|
|
|
|
|
| 101 |
],
|
| 102 |
+
outputs=[result],
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|