import gradio as gr import requests import os # 使用 Dreamlike 模型來生成圖像 API_URL = "https://api-inference.huggingface.co/models/dreamlike-art/dreamlike-photoreal-2.0" HF_API_TOKEN = os.getenv("HF_API_TOKEN") headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) if response.status_code != 200: print(f"Request failed with status code {response.status_code}") print(f"Error details: {response.text}") return f"Error: {response.status_code}, {response.text}" return response.content # 返回圖片的二進位數據 def generate_dinner_image(hint): # 使用繁體中文提示對應的晚餐描述 dinner_hints = { "這是一個有很多起司的東西,通常切成片狀。": "a pizza with lots of cheese", "這是一道義大利麵,通常搭配奶油醬。": "a plate of creamy spaghetti", "這是一個烤的東西,有些人喜歡加上烤肉醬。": "grilled chicken with barbecue sauce", "這通常在一個麵包中,裡面有生菜和番茄。": "a hamburger with lettuce and tomato", "這是亞洲常見的食物,通常搭配醬油。": "sushi with soy sauce" } description = dinner_hints.get(hint, "delicious food") # 調用 API 生成圖像 output = query({"inputs": description}) if isinstance(output, str) and output.startswith("Error:"): return output # 返回錯誤訊息 # 保存生成的圖像到文件 with open("generated_dinner.png", "wb") as f: f.write(output) return "generated_dinner.png" # 創建 Gradio 介面 interface = gr.Interface( fn=generate_dinner_image, inputs=gr.Textbox(lines=2, placeholder="輸入提示內容來生成晚餐圖片...(例如:這是一個有很多起司的東西,通常切成片狀。)"), outputs="image", title="最強第一組", description="根據您的描述生成晚餐圖片!" ) # 啟動 Gradio 應用 interface.launch()