File size: 2,545 Bytes
9882e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# import gradio as gr

# def generate_hoverable_html(text):
#     # 分割文本为单词
#     words = text.split()
    
#     # 为每个单词创建一个带有悬停信息的 HTML span 元素
#     html_words = [
#         f'<span class="hoverable-word" data-info="Information about {word}">{word}</span>'
#         for word in words
#     ]
    
#     # 将单词合并回字符串
#     hoverable_html = ' '.join(html_words)
    
#     # 添加 CSS 和 JavaScript
#     custom_html = f"""
#     <style>
#         .hoverable-word {{
#             color: blue;
#             cursor: pointer;
#         }}
#         .hoverable-word:hover::after {{
#             content: attr(data-info);
#             color: white;
#             background-color: black;
#             padding: 4px;
#             margin-left: 8px;
#             position: absolute;
#         }}
#     </style>
#     <div>{hoverable_html}</div>
#     """
    
#     return custom_html

# # 创建 Gradio 界面
# with gr.Blocks() as demo:
#     with gr.Row():
#         text_input = gr.Textbox(label="Input Text", placeholder="Type here...")
#     output_html = gr.HTML()
    
#     # 连接输入、处理函数和输出
#     text_input.change(generate_hoverable_html, text_input, output_html)

# demo.launch()

import gradio as gr

def generate_hoverable_html(text):
    # 分割文本为单词
    words = text.split()
    prob_dic={'a':{'b':0.1,'c':0.2},'b':{'a':0.1,'c':0.2}}
    # 为每个单词创建一个带有悬停信息的 HTML span 元素
    html_words = [
        f'<span class="hoverable-word" data-info="{prob_dic[word]}">{word}</span>'
        for word in words
    ]
    
    # 将单词合并回字符串
    hoverable_html = ' '.join(html_words)
    
    # 添加 CSS 和 JavaScript
    custom_html = f"""
    <style>
        .hoverable-word {{
            color: blue;
            cursor: pointer;
        }}
        .hoverable-word:hover::after {{
            content: attr(data-info);
            color: white;
            background-color: black;
            padding: 4px;
            margin-left: 8px;
            position: absolute;
        }}
    </style>
    <div>{hoverable_html}</div>
    """
    
    return custom_html

# 创建 Gradio 界面
with gr.Blocks() as demo:
    with gr.Row():
        text_input = gr.Textbox(label="Input Text", placeholder="Type here...")
    output_html = gr.HTML()
    
    # 连接输入、处理函数和输出
    text_input.change(generate_hoverable_html, text_input, output_html)

demo.launch(debug=True)