Spaces:
Running
Running
feifeinoban
commited on
Commit
·
c3c15e0
1
Parent(s):
933ea3b
Fix image path for Gradio rendering
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
with open("README.md", "r", encoding="utf-8") as f:
|
| 5 |
content = f.read()
|
| 6 |
|
|
@@ -10,9 +12,28 @@ def load_markdown():
|
|
| 10 |
if len(parts) >= 3:
|
| 11 |
content = parts[2].strip()
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
with gr.Blocks() as demo:
|
| 16 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import markdown
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
+
def convert_markdown_to_html():
|
| 6 |
with open("README.md", "r", encoding="utf-8") as f:
|
| 7 |
content = f.read()
|
| 8 |
|
|
|
|
| 12 |
if len(parts) >= 3:
|
| 13 |
content = parts[2].strip()
|
| 14 |
|
| 15 |
+
# 修复图片路径为绝对URL
|
| 16 |
+
def fix_image_paths(match):
|
| 17 |
+
alt_text = match.group(1) or ""
|
| 18 |
+
path = match.group(2)
|
| 19 |
+
|
| 20 |
+
if not path.startswith(("http://", "https://")):
|
| 21 |
+
path = f"https://huggingface.co/spaces/feifeinoban/shell/resolve/main/{path.lstrip('./')}"
|
| 22 |
+
|
| 23 |
+
return f''
|
| 24 |
+
|
| 25 |
+
content = re.sub(r'!\[(.*?)\]\((.*?)\)', fix_image_paths, content)
|
| 26 |
+
|
| 27 |
+
# 将Markdown转换为HTML
|
| 28 |
+
html_content = markdown.markdown(content, extensions=['tables', 'fenced_code'])
|
| 29 |
+
|
| 30 |
+
return html_content
|
| 31 |
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
+
gr.HTML("""
|
| 34 |
+
<div style="max-width: 900px; margin: 0 auto; padding: 20px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;">
|
| 35 |
+
""")
|
| 36 |
+
gr.HTML(convert_markdown_to_html())
|
| 37 |
+
gr.HTML("</div>")
|
| 38 |
|
| 39 |
demo.launch()
|