GiantPandas commited on
Commit
b492cbd
·
verified ·
1 Parent(s): 22afd03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -5,13 +5,22 @@ import gradio as gr
5
 
6
 
7
  def files_to_b64(files):
8
- """把上传的多张图片转成 base64 列表,以便前端快速切换。"""
9
- b64_list = []
10
  for f in files:
11
- suffix = pathlib.Path(f.name).suffix[1:]
12
- data = base64.b64encode(f.read()).decode()
13
- b64_list.append(f"data:image/{suffix};base64,{data}")
14
- return b64_list
 
 
 
 
 
 
 
 
 
15
 
16
 
17
  def render_img(b64_list, idx, scale):
 
5
 
6
 
7
  def files_to_b64(files):
8
+ """支持 Gradio NamedString / FileData / 纯路径字符串"""
9
+ out = []
10
  for f in files:
11
+ # Gradio 4.x 的 FileData / NamedString
12
+ if hasattr(f, "data"): # NamedString / FileData
13
+ raw = f.data # bytes
14
+ suffix = pathlib.Path(f.name).suffix[1:]
15
+ # ② 早期 gr.File 可能直接给文件路径字符串
16
+ else:
17
+ path = pathlib.Path(f)
18
+ raw = path.read_bytes()
19
+ suffix = path.suffix[1:]
20
+
21
+ b64 = base64.b64encode(raw).decode()
22
+ out.append(f"data:image/{suffix};base64,{b64}")
23
+ return out
24
 
25
 
26
  def render_img(b64_list, idx, scale):