feifeinoban commited on
Commit
5b546ca
·
1 Parent(s): e55329c

Fix image path for Gradio rendering

Browse files
Files changed (1) hide show
  1. app.py +41 -20
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import re
3
  import os
 
4
 
5
  def load_and_fix_markdown():
6
  with open("README.md", "r", encoding="utf-8") as f:
@@ -12,36 +13,56 @@ def load_and_fix_markdown():
12
  if len(parts) >= 3:
13
  content = parts[2].strip()
14
 
15
- # 修复所有图片路径
16
- def replace_path(match):
17
  alt_text = match.group(1) or ""
18
  path = match.group(2)
19
 
20
- # 如果已经是完整URL,保持不变
21
- if path.startswith(("http://", "https://")):
22
- return f'![{alt_text}]({path})'
23
-
24
- # 处理相对路径
25
  clean_path = path.lstrip('./')
26
 
27
- # 确保路径存在
 
28
  if os.path.exists(clean_path):
29
- return f'![{alt_text}](/file={clean_path})'
30
  else:
31
- # 如果文件不存在,尝试在assets目录中查找
32
  filename = os.path.basename(clean_path)
33
- assets_path = f"assets/{filename}"
34
- if os.path.exists(assets_path):
35
- return f'![{alt_text}](/file={assets_path})'
36
- else:
37
- # 文件不存在,返回原始路径(可能会有警告)
38
- return f'![{alt_text}]({path})'
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- content = re.sub(r'!\[(.*?)\]\((.*?)\)', replace_path, content)
41
  return content
42
 
43
  with gr.Blocks() as demo:
44
- gr.Markdown(load_and_fix_markdown())
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # 允许访问当前目录和可能的assets目录
47
- demo.launch(allowed_paths=["./", "./assets"])
 
1
  import gradio as gr
2
  import re
3
  import os
4
+ import base64
5
 
6
  def load_and_fix_markdown():
7
  with open("README.md", "r", encoding="utf-8") as f:
 
13
  if len(parts) >= 3:
14
  content = parts[2].strip()
15
 
16
+ # 方法1: 使用HTML img标签
17
+ def replace_with_html(match):
18
  alt_text = match.group(1) or ""
19
  path = match.group(2)
20
 
21
+ # 处理本地文件路径
 
 
 
 
22
  clean_path = path.lstrip('./')
23
 
24
+ # 尝试找到实际文件
25
+ actual_path = None
26
  if os.path.exists(clean_path):
27
+ actual_path = clean_path
28
  else:
29
+ # 尝试在assets目录中查找
30
  filename = os.path.basename(clean_path)
31
+ possible_paths = [
32
+ f"assets/{filename}",
33
+ f"assets/{clean_path}",
34
+ f"images/{filename}",
35
+ filename
36
+ ]
37
+ for p in possible_paths:
38
+ if os.path.exists(p):
39
+ actual_path = p
40
+ break
41
+
42
+ if actual_path:
43
+ # 使用HTML img标签
44
+ return f'<img src="/file={actual_path}" alt="{alt_text}" style="max-width: 100%; height: auto; display: block; margin: 10px 0;">'
45
+ else:
46
+ # 如果找不到文件,保留原始URL(可能是外部URL)
47
+ return f'![{alt_text}]({path})'
48
 
49
+ content = re.sub(r'!\[(.*?)\]\((.*?)\)', replace_with_html, content)
50
  return content
51
 
52
  with gr.Blocks() as demo:
53
+ # 使用HTML组件而不是Markdown组件
54
+ gr.HTML("""
55
+ <div style="max-width: 800px; margin: 0 auto; padding: 20px;">
56
+ <div id="markdown-content">
57
+ """)
58
+
59
+ # 动态加载内容
60
+ gr.HTML(load_and_fix_markdown())
61
+
62
+ gr.HTML("""
63
+ </div>
64
+ </div>
65
+ """)
66
 
67
+ # 确保允许访问所有可能的路径
68
+ demo.launch(allowed_paths=["./", "./assets", "./images", "./static"])