kenfoo commited on
Commit
2bed509
·
verified ·
1 Parent(s): 3bfea16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -18
app.py CHANGED
@@ -23,22 +23,42 @@ LORA_STYLES = [
23
  MAX_SEED = 2**31 - 1
24
 
25
 
26
- def upload_to_imgbb(image_path):
27
- """上传图片到免费图床,返回公开 URL"""
28
- IMGBB_API_KEY = os.environ.get("IMGBB_API_KEY")
29
- if not IMGBB_API_KEY:
30
- return None
31
- with open(image_path, "rb") as f:
32
- import base64
33
- b64 = base64.b64encode(f.read()).decode()
34
- resp = requests.post(
35
- "https://api.imgbb.com/1/upload",
36
- data={"key": IMGBB_API_KEY, "image": b64}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  )
38
- if resp.status_code == 200:
39
- print(f'图床resp.json()={resp.json()}')
40
- return resp.json()["data"]["url"]
41
- return None
42
 
43
 
44
  def infer(
@@ -59,9 +79,12 @@ def infer(
59
  seed = random.randint(0, MAX_SEED)
60
 
61
  # 方案:上传到公开图床,拿到 URL 再传给远端
62
- public_url = upload_to_imgbb(image)
 
 
 
63
  if not public_url:
64
- print("图床上传失败,请检查 IMGBB_API_KEY")
65
  return None, seed
66
 
67
  print(f"图片公开 URL: {public_url}")
@@ -69,7 +92,7 @@ def infer(
69
  # 直接传 URL 字符串,让远端自己下载
70
  # 直接传字符串列表
71
  images_input = [public_url]
72
-
73
  print(f"prompt: {prompt}, lora: {lora_adapter}, seed: {seed}")
74
 
75
  try:
 
23
  MAX_SEED = 2**31 - 1
24
 
25
 
26
+ # def upload_to_imgbb(image_path):
27
+ # """上传图片到免费图床,返回公开 URL"""
28
+ # IMGBB_API_KEY = os.environ.get("IMGBB_API_KEY")
29
+ # if not IMGBB_API_KEY:
30
+ # return None
31
+ # with open(image_path, "rb") as f:
32
+ # import base64
33
+ # b64 = base64.b64encode(f.read()).decode()
34
+ # resp = requests.post(
35
+ # "https://api.imgbb.com/1/upload",
36
+ # data={"key": IMGBB_API_KEY, "image": b64}
37
+ # )
38
+ # if resp.status_code == 200:
39
+ # print(f'图床resp.json()={resp.json()}')
40
+ # return resp.json()["data"]["url"]
41
+ # return None
42
+
43
+ def upload_to_hf(image_path):
44
+ """上传到 HF dataset,返回公开直链"""
45
+ from huggingface_hub import HfApi
46
+ import uuid
47
+
48
+ api = HfApi(token=HF_TOKEN)
49
+ filename = f"{uuid.uuid4().hex[:8]}_{os.path.basename(image_path)}"
50
+
51
+ # 上传到你自己的 HF dataset repo(需要先创建一个 public dataset repo)
52
+ url = api.upload_file(
53
+ path_or_fileobj=image_path,
54
+ path_in_repo=f"uploads/{filename}",
55
+ repo_id="kenfoo", # 改成你自己的
56
+ repo_type="dataset",
57
  )
58
+ print(f'图床 ={url}')
59
+ # 转成直链格式
60
+ # url 格式: https://huggingface.co/datasets/xxx/yyy/resolve/main/uploads/zzz.jpg
61
+ return url
62
 
63
 
64
  def infer(
 
79
  seed = random.randint(0, MAX_SEED)
80
 
81
  # 方案:上传到公开图床,拿到 URL 再传给远端
82
+ #public_url = upload_to_imgbb(image)
83
+
84
+ public_url = upload_to_hf(image)
85
+
86
  if not public_url:
87
+ print("图床上传失败 ")
88
  return None, seed
89
 
90
  print(f"图片公开 URL: {public_url}")
 
92
  # 直接传 URL 字符串,让远端自己下载
93
  # 直接传字符串列表
94
  images_input = [public_url]
95
+
96
  print(f"prompt: {prompt}, lora: {lora_adapter}, seed: {seed}")
97
 
98
  try: