Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# 加载模型和分词器
|
6 |
+
model_name = "jinaai/reader-lm-1.5b"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to("cuda")
|
9 |
+
|
10 |
+
# 定义转换函数
|
11 |
+
def html_to_markdown(html_content):
|
12 |
+
messages = [{"role": "user", "content": html_content}]
|
13 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False)
|
14 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to("cuda")
|
15 |
+
outputs = model.generate(inputs, max_new_tokens=1024, temperature=0, do_sample=False, repetition_penalty=1.08)
|
16 |
+
markdown_content = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
17 |
+
return markdown_content
|
18 |
+
|
19 |
+
# 创建 Gradio 接口
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=html_to_markdown,
|
22 |
+
inputs=gr.inputs.Textbox(lines=20, label="HTML 内容"),
|
23 |
+
outputs=gr.outputs.Textbox(label="Markdown 内容"),
|
24 |
+
title="HTML 转 Markdown 转换器",
|
25 |
+
description="输入 HTML 内容,模型将其转换为 Markdown 格式。",
|
26 |
+
)
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
iface.launch()
|