Spaces:
Sleeping
Sleeping
File size: 5,340 Bytes
06b8797 ebcfb0d d141ac8 06b8797 bf6edf9 06b8797 d09190e 06b8797 63d6796 d09190e 06b8797 31684d8 8f60829 06b8797 a6cb3c9 bf6edf9 a6cb3c9 bf6edf9 a6cb3c9 bf6edf9 a6cb3c9 bf6edf9 a6cb3c9 06b8797 a6cb3c9 06b8797 bf6edf9 06b8797 bf6edf9 06b8797 13841ca bf6edf9 06b8797 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# app.py
import gradio as gr
import together
import os
import base64
from PIL import Image
from io import BytesIO
import requests # ▼▼▼ 修正箇所: requestsライブラリをインポート ▼▼▼
# --- 1. 設定: APIキーの読み込み ---
api_key = os.environ.get("TOGETHER_API_KEY")
if api_key:
together_client = together.Together(api_key=api_key)
else:
together_client = None
# 使用する画像生成モデル
IMAGE_MODEL = "black-forest-labs/FLUX.1-schnell-Free"
# --- 2. 中核機能: 画像生成関数 ---
def generate_image_from_prompt(prompt: str, history: list):
if not together_client:
raise gr.Error("TogetherAIのAPIクライアントが設定されていません。環境変数 TOGETHER_API_KEY を確認してください。")
if not prompt or not prompt.strip():
raise gr.Error("画像を生成するには、プロンプトを入力してください。")
print(f"プロンプトを元に画像を生成中: '{prompt}'")
try:
response = together_client.images.generate(
prompt=prompt,
model=IMAGE_MODEL,
steps=4,
n=4,
height=1024,
width=1024
)
generated_images = []
# ▼▼▼ 修正箇所: URLから画像をダウンロードするロジックに変更 ▼▼▼
for image_data_item in response.data:
# APIの応答に 'url' が含まれているかチェック
if hasattr(image_data_item, 'url') and image_data_item.url:
try:
image_url = image_data_item.url
print(f"URLから画像をダウンロード中: {image_url}")
# URLにアクセスして画像データを取得
image_response = requests.get(image_url)
image_response.raise_for_status() # エラーがあれば例外を発生させる
# ダウンロードしたバイトデータからPILイメージを作成
image = Image.open(BytesIO(image_response.content))
generated_images.append((image, prompt))
except requests.exceptions.RequestException as e:
print(f"画像のダウンロードに失敗しました: {e}")
continue # この画像の処理をスキップして次に進む
# (参考) b64_jsonが返ってくるモデルのためのフォールバック処理
elif image_data_item.b64_json:
image_b64 = image_data_item.b64_json
image_data = base64.b64decode(image_b64)
image = Image.open(BytesIO(image_data))
generated_images.append((image, prompt))
if not generated_images:
raise gr.Error("画像の生成に失敗しました。APIから有効な画像データやURLが返されませんでした。")
new_history = generated_images + history
print(f"{len(generated_images)}枚の画像の生成が完了しました。")
return new_history, new_history
except Exception as e:
if isinstance(e, gr.Error):
raise e
print(f"予期せぬエラーが発生しました: {e}")
raise gr.Error(f"画像の生成に失敗しました。エラー詳細: {e}")
# --- 3. Gradio UIの構築 (変更なし) ---
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px; margin: auto;}") as app:
gr.Markdown(
"""
# 🖼️ TogetherAI 画像生成アプリ (Gradio版)
プロンプトを入力して、TogetherAIのAPIで画像を生成します。
最新の画像がギャラリーの先頭に表示されます。
"""
)
gr.Markdown("---")
history_state = gr.State([])
with gr.Column():
prompt_input = gr.Textbox(
label="画像生成プロンプト",
placeholder="例: 月面にいる、写真のようにリアルな猫の宇宙飛行士",
lines=4,
interactive=True,
)
if not together_client:
gr.Warning("環境変数 `TOGETHER_API_KEY` が見つかりません。画像生成機能を利用するにはAPIキーを設定してください。")
generate_btn = gr.Button("画像生成 ✨ (4枚)", variant="primary", interactive=(together_client is not None))
gr.Markdown("---")
gr.Markdown("## 生成された画像")
image_gallery = gr.Gallery(label="生成結果", show_label=False, columns=2, height="auto", object_fit="contain", value=None, preview=True)
generate_btn.click(fn=generate_image_from_prompt, inputs=[prompt_input, history_state], outputs=[image_gallery, history_state], show_progress="full")
gr.Examples(
examples=[
"A high-resolution photo of a futuristic city with flying cars",
"An oil painting of a serene landscape with a river and mountains",
"A cute, fluffy alien creature exploring a vibrant, magical forest",
"Logo for a coffee shop named 'The Daily Grind', minimalist, modern"
],
inputs=prompt_input
)
if __name__ == "__main__":
app.launch(mcp_server=True) |