tsengiii commited on
Commit
2bbae9c
·
verified ·
1 Parent(s): 728c16a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -16
app.py CHANGED
@@ -1,16 +1,10 @@
1
- import os
2
-
3
- # 測試是否能正確讀取 Hugging Face API token
4
- HF_API_TOKEN = os.getenv("HF_API_TOKEN")
5
- print(f"Your API Token: {HF_API_TOKEN}")
6
-
7
  import gradio as gr
8
  import requests
9
  import os
10
 
11
- # 使用 Stable Diffusion 來生成晚餐圖片
12
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
13
- HF_API_TOKEN = os.getenv("HF_API_TOKEN") # 確保您已在 Hugging Face Spaces 的 Secret 中設定 API token
14
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
15
 
16
  def query(payload):
@@ -24,7 +18,7 @@ def query(payload):
24
  return response.content # 返回圖片的二進位數據
25
 
26
  def generate_dinner_image(hint):
27
- # 使用繁體中文的提示內容來對應不同的晚餐圖片描述
28
  dinner_hints = {
29
  "這是一個有很多起司的東西,通常切成片狀。": "a pizza with lots of cheese",
30
  "這是一道義大利麵,通常搭配奶油醬。": "a plate of creamy spaghetti",
@@ -33,30 +27,27 @@ def generate_dinner_image(hint):
33
  "這是亞洲常見的食物,通常搭配醬油。": "sushi with soy sauce"
34
  }
35
 
36
- # 根據提示生成晚餐描述
37
  description = dinner_hints.get(hint, "delicious food")
38
 
39
  # 調用 API 生成圖像
40
  output = query({"inputs": description})
41
 
42
- # 如果返回錯誤信息,顯示錯誤
43
  if isinstance(output, str) and output.startswith("Error:"):
44
- return output # 直接返回錯誤訊息
45
 
46
  # 保存生成的圖像到文件
47
  with open("generated_dinner.png", "wb") as f:
48
  f.write(output)
49
 
50
- # 返回生成的圖像文件
51
  return "generated_dinner.png"
52
 
53
  # 創建 Gradio 介面
54
  interface = gr.Interface(
55
  fn=generate_dinner_image,
56
  inputs=gr.Textbox(lines=2, placeholder="輸入提示內容來生成晚餐圖片...(例如:這是一個有很多起司的東西,通常切成片狀。)"),
57
- outputs="image", # 生成圖片
58
  title="最強第一組",
59
- description="根據您的描述生成晚餐圖片!輸入提示內容(例如:這是一個有很多起司的東西,通常切成片狀)"
60
  )
61
 
62
  # 啟動 Gradio 應用
 
 
 
 
 
 
 
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):
 
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",
 
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 應用