File size: 7,386 Bytes
57a06ba
 
b83454d
 
57a06ba
b83454d
57a06ba
 
 
b83454d
4153034
b83454d
4153034
 
b83454d
e6d2adb
 
b83454d
 
 
 
e6d2adb
b83454d
 
 
 
 
e6d2adb
b83454d
 
e6d2adb
b83454d
e6d2adb
 
b83454d
 
 
e6d2adb
b83454d
 
 
 
 
57a06ba
b83454d
 
 
57a06ba
b83454d
e6d2adb
 
b83454d
 
 
 
 
 
e6d2adb
 
b83454d
 
 
e6d2adb
 
b83454d
57a06ba
 
 
b83454d
 
57a06ba
e6d2adb
 
b83454d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a06ba
e6d2adb
b83454d
 
 
 
 
 
e6d2adb
b83454d
 
 
 
 
 
 
 
 
e6d2adb
 
b83454d
e6d2adb
b83454d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a06ba
 
 
e6d2adb
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# app.py
import torch
import streamlit as st
from transformers import AutoModel

st.set_page_config(page_title="Semantic Highlight Bilingual Demo", layout="wide")


@st.cache_resource(show_spinner=True)
def load_model():
    model = AutoModel.from_pretrained(
        "zilliz/semantic-highlight-bilingual-v1",
        trust_remote_code=True,
    )
    return model


def split_sentences(text: str):
    text = text.strip()
    if not text:
        return []

    # Very simple heuristic: use Chinese period if present, else English period.
    if "。" in text:
        parts = [s.strip() for s in text.split("。") if s.strip()]
        # Add back "。" to each sentence for nicer display.
        sentences = [s + "。" for s in parts]
    else:
        parts = [s.strip() for s in text.split(".") if s.strip()]
        sentences = [s + "." for s in parts]

    return sentences


def highlight_context(context: str, highlighted_sentences):
    if not context or not highlighted_sentences:
        return context

    # Simple HTML highlighting by sentence replacement
    highlighted_html = context
    for sent in highlighted_sentences:
        sent_clean = sent.strip()
        if not sent_clean:
            continue
        # Avoid double-wrapping: only replace plain text, not already highlighted
        replacement = f'<span class="hl-sentence">{sent_clean}</span>'
        highlighted_html = highlighted_html.replace(sent_clean, replacement)

    # Basic styling
    style = """
    <style>
    .hl-sentence {
        background-color: rgba(255, 215, 0, 0.35);
        padding: 2px 3px;
        border-radius: 3px;
    }
    .context-box {
        white-space: pre-wrap;
        font-family: ui-monospace, Menlo, Monaco, "Courier New", monospace;
        font-size: 0.9rem;
        line-height: 1.5;
    }
    </style>
    """
    return style + f'<div class="context-box">{highlighted_html}</div>'


def main():
    st.title("Semantic Highlight Bilingual Demo")
    st.caption("Model: zilliz/semantic-highlight-bilingual-v1")

    with st.sidebar:
        st.header("Settings")
        threshold = st.slider(
            "Relevance threshold",
            min_value=0.0,
            max_value=1.0,
            value=0.5,
            step=0.01,
            help="Lower values highlight more sentences; higher values highlight fewer.",
        )
        language = st.selectbox(
            "Language",
            options=["auto", "en", "zh"],
            index=0,
            help="Let the model auto-detect, or force English (en) / Chinese (zh).",
        )
        return_sentence_metrics = st.checkbox(
            "Return per-sentence probabilities",
            value=True,
        )

        st.markdown("---")
        st.info(
            "1. Enter a query.\n"
            "2. Paste a document as context.\n"
            "3. Click **Run Semantic Highlight**."
        )

    default_question = "What are the symptoms of dehydration?"
    default_context = (
        "Dehydration occurs when your body loses more fluid than you take in.\n"
        "Common signs include feeling thirsty and having a dry mouth.\n"
        "The human body is composed of about 60% water.\n"
        "Dark yellow urine and infrequent urination are warning signs.\n"
        "Water is essential for many bodily functions.\n"
        "Dizziness, fatigue, and headaches can indicate severe dehydration.\n"
        "Drinking enough water daily is often recommended."
    )

    col_left, col_right = st.columns(2)

    with col_left:
        question = st.text_input(
            "Query / Question",
            value=default_question,
        )
        context = st.text_area(
            "Context / Document",
            value=default_context,
            height=260,
        )

    with col_right:
        st.subheader("Controls")
        run = st.button("Run Semantic Highlight", type="primary")

        if run:
            if not question.strip():
                st.error("Please enter a query/question.")
                return
            if not context.strip():
                st.error("Please enter some context text.")
                return

            with st.spinner("Loading model and running inference..."):
                model = load_model()
                kwargs = {
                    "question": question,
                    "context": context,
                    "threshold": threshold,
                    "return_sentence_metrics": return_sentence_metrics,
                }
                if language != "auto":
                    kwargs["language"] = language

                with torch.no_grad():
                    result = model.process(**kwargs)

            highlighted_sentences = result.get("highlighted_sentences", [])
            compression_rate = result.get("compression_rate", None)
            sentence_probs = result.get("sentence_probabilities", None)

            st.subheader("Results")

            # Metrics row
            metric_cols = st.columns(3)
            with metric_cols[0]:
                st.metric(
                    "Highlighted sentences",
                    value=len(highlighted_sentences),
                )
            with metric_cols[1]:
                if compression_rate is not None:
                    st.metric(
                        "Compression rate",
                        value=f"{compression_rate * 100:.1f}%",
                        help="Approximate percentage of text removed.",
                    )
            with metric_cols[2]:
                st.metric(
                    "Threshold used",
                    value=f"{threshold:.2f}",
                )

            # Highlighted sentence list
            st.markdown("### Highlighted Sentences")
            if highlighted_sentences:
                for i, sent in enumerate(highlighted_sentences, start=1):
                    st.markdown(f"**{i}.** {sent}")
            else:
                st.write("No sentences passed the current threshold.")

            # Full context with inline highlights
            st.markdown("### Context with Highlights")
            highlighted_html = highlight_context(context, highlighted_sentences)
            st.markdown(highlighted_html, unsafe_allow_html=True)

            # Sentence probabilities table (if available)
            if return_sentence_metrics and sentence_probs is not None:
                st.markdown("### Sentence Probabilities")

                sentences = split_sentences(context)
                # Align lengths if possible; otherwise just show probabilities
                if len(sentences) == len(sentence_probs):
                    import pandas as pd

                    data = {
                        "Sentence #": list(range(1, len(sentences) + 1)),
                        "Sentence": sentences,
                        "Probability": sentence_probs,
                    }
                    df = pd.DataFrame(data)
                    st.dataframe(
                        df,
                        use_container_width=True,
                    )
                else:
                    st.write(
                        "Count of split sentences does not match model probabilities; "
                        "showing raw probability list."
                    )
                    st.write(sentence_probs)


if __name__ == "__main__":
    main()