Peiiiiiiiiru commited on
Commit
a132a03
·
verified ·
1 Parent(s): cae62a9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import io
4
+ from PIL import Image
5
+ from dotenv import load_dotenv
6
+ import os
7
+
8
+ # 載入環境變數
9
+ load_dotenv()
10
+
11
+ # Hugging Face API 設定
12
+ API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
13
+ headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
14
+
15
+ def generate_image(prompt):
16
+ try:
17
+ # 呼叫 Hugging Face API
18
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
19
+ # 轉換回應為圖像
20
+ image = Image.open(io.BytesIO(response.content))
21
+ return image
22
+ except Exception as e:
23
+ return None
24
+
25
+ # 建立 Gradio 介面
26
+ iface = gr.Interface(
27
+ fn=generate_image,
28
+ inputs=gr.Textbox(label="請輸入圖像描述"),
29
+ outputs=gr.Image(label="生成的圖像"),
30
+ title="浮世繪風格圖像生成器",
31
+ description="輸入描述文字,生成浮世繪風格的圖像"
32
+ )
33
+
34
+ # 啟動應用
35
+ iface.launch()