File size: 1,621 Bytes
29db73d
 
b18eeee
22920e8
c9c01b7
191867f
c9c01b7
9376aef
b2c2ee0
bb32ae4
 
 
4b6e8f7
bb32ae4
 
 
 
 
fb2d59a
bb32ae4
 
 
 
 
 
 
 
4b6e8f7
 
29db73d
15748f6
 
 
 
4b6e8f7
bb32ae4
 
4b6e8f7
bb32ae4
 
 
 
 
 
4b6e8f7
bb32ae4
 
 
 
 
 
 
 
 
 
4b6e8f7
bb32ae4
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
import time  # 模拟处理耗时
import os
import spacy
from spacy import displacy
import pandas as pd

# os.system("python -m spacy download en_core_web_md")

# nlp = spacy.load("en_core_web_md")
# def process_api(input_text):
#     # 这里编写实际的后端处理逻辑

#     return {
#         "status": "success",
#         "result": f"Processed: {input_text.upper()}",
#         "timestamp": time.time()
#     }

# # 设置API格式为JSON
# gr.Interface(
#     fn=process_api,
#     inputs="text",
#     outputs="json",
#     title="Backend API",
#     allow_flagging="never"
# ).launch()


# import gradio as gr
# import spacy
# from spacy import displacy
# import pandas as pd
# import time

nlp = spacy.load("en_core_web_md")
HTML_WRAPPER = "<div style='padding: 10px;'>{}</div>"

def show_spatial_ent_table(doc):
    rows = []
    for i, ent in enumerate(doc.ents):
        rows.append(f"<tr><td>{i+1}</td><td>{ent.text}</td><td>{ent.label_}</td></tr>")
    table_html = "<table border='1'><tr><th>Index</th><th>Entity</th><th>Label</th></tr>" + "".join(rows) + "</table>"
    return table_html

def process_api(input_text):
    doc = nlp(input_text)
    html_ent = displacy.render(doc, style="ent")
    html_ent = HTML_WRAPPER.format(html_ent.replace("\n", ""))
    html_table = show_spatial_ent_table(doc)
    final_html = html_ent + "<br>" + html_table
    return {
        "data": [{"html": final_html}],
        "timestamp": time.time()
    }

gr.Interface(
    fn=process_api,
    inputs="text",
    outputs="json",
    allow_flagging="never",
    title="Backend API"
).launch()