tsengiii commited on
Commit
70d04d1
·
verified ·
1 Parent(s): 2bbae9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -45
app.py CHANGED
@@ -1,54 +1,35 @@
1
  import gradio as gr
2
  import requests
 
 
 
3
  import os
4
 
5
- # 使用 Dreamlike 模型來生成圖像
6
- API_URL = "https://api-inference.huggingface.co/models/dreamlike-art/dreamlike-photoreal-2.0"
7
- HF_API_TOKEN = os.getenv("HF_API_TOKEN")
8
- headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
9
 
10
- def query(payload):
11
- response = requests.post(API_URL, headers=headers, json=payload)
12
-
13
- if response.status_code != 200:
14
- print(f"Request failed with status code {response.status_code}")
15
- print(f"Error details: {response.text}")
16
- return f"Error: {response.status_code}, {response.text}"
17
-
18
- return response.content # 返回圖片的二進位數據
19
 
20
- def generate_dinner_image(hint):
21
- # 使用繁體中文提示對應的晚餐描述
22
- dinner_hints = {
23
- "這是一個有很多起司的東西,通常切成片狀。": "a pizza with lots of cheese",
24
- "這是一道義大利麵,通常搭配奶油醬。": "a plate of creamy spaghetti",
25
- "這是一個烤的東西,有些人喜歡加上烤肉醬。": "grilled chicken with barbecue sauce",
26
- "這通常在一個麵包中,裡面有生菜和番茄。": "a hamburger with lettuce and tomato",
27
- "這是亞洲常見的食物,通常搭配醬油。": "sushi with soy sauce"
28
- }
29
-
30
- description = dinner_hints.get(hint, "delicious food")
31
-
32
- # 調用 API 生成圖像
33
- output = query({"inputs": description})
34
-
35
- if isinstance(output, str) and output.startswith("Error:"):
36
- return output # 返回錯誤訊息
37
-
38
- # 保存生成的圖像到文件
39
- with open("generated_dinner.png", "wb") as f:
40
- f.write(output)
41
-
42
- return "generated_dinner.png"
43
 
44
- # 創建 Gradio 介面
45
- interface = gr.Interface(
46
- fn=generate_dinner_image,
47
- inputs=gr.Textbox(lines=2, placeholder="輸入提示內容來生成晚餐圖片...(例如:這是一個有很多起司的東西,通常切成片狀。)"),
48
- outputs="image",
49
- title="最強第一組",
50
- description="根據您的描述生成晚餐圖片!"
51
  )
52
 
53
- # 啟動 Gradio 應用
54
- interface.launch()
 
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": "Bearer hf_MySpaceToken"}
 
 
 
 
 
 
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()