Spaces:
Sleeping
Sleeping
File size: 5,136 Bytes
d952fbe |
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 |
import gradio as gr
import colorsys
from functools import partial
import random
def lighten_color(hex_color, factor=1.8):
"""Lightens a HEX color by increasing its brightness in HSV space."""
hex_color = hex_color.lstrip("#")
r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
# Convert to HSV
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
v = min(1.0, v * factor) # Increase brightness
# Convert back to HEX
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, s, v)]
return f'#{r:02x}{g:02x}{b:02x}'
def darken_color(hex_color, factor=0.7):
"""Darkens a hex color by reducing its brightness in the HSV space."""
hex_color = hex_color.lstrip("#")
r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
# Convert to HSV to adjust brightness
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
v = max(0, v * factor) # Reduce brightness
# Convert back to HEX
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, s, v)]
return f'#{r:02x}{g:02x}{b:02x}'
# Generate unique colors for pairs
def generate_color(index, total_colors=20):
"""Generates a unique, evenly spaced color for each index using HSL."""
hue = index / total_colors # Spread hues in range [0,1]
saturation = 0.65 # Keep colors vivid
lightness = 0.75 # Balanced brightness
# Convert HSL to RGB
r, g, b = colorsys.hls_to_rgb(hue, lightness, saturation)
r, g, b = int(r * 255), int(g * 255), int(b * 255)
return f'#{r:02x}{g:02x}{b:02x}' # Convert to hex
def highlight_pairs(text1, text2):
"""Highlight matching pairs between two paragraphs"""
# Predefined matching pairs
match_pairs = [
{"index": 1, "text1": "deep learning", "start1": 13, "end1": 26,
"text2": "deep learning", "start2": 12, "end2": 25},
{"index": 2, "text1": "neural networks", "start1": 56, "end1": 71,
"text2": "neural networks", "start2": 68, "end2": 83},
{"index": 3, "text1": "AI research", "start1": 86, "end1": 97,
"text2": "AI research", "start2": 55, "end2": 66},
]
# Assign unique colors to each index
pair_colors = {pair["index"]: generate_color(pair["index"], total_colors=len(match_pairs)) for pair in match_pairs}
def apply_highlight(text, pairs, key_start, key_end, key_index, pair_colors):
highlighted_text = ""
prev_end = 0
for pair in sorted(pairs, key=lambda x: x[key_start]):
start, end, index = pair[key_start], pair[key_end], pair[key_index]
color = pair_colors.get(index, "#ddd") # Default color if not found
color = lighten_color(color, factor=2.2) # Lightened color for background text
label_color = darken_color(color, factor=0.7) # Make label color darker
# Style the index as a label
index_label = (f'<span style="background-color:{label_color}; color:white; '
f'padding:1px 4px; border-radius:4px; font-size:12px; '
f'font-weight:bold; display:inline-block; margin-right:4px;">{index}</span>')
# Append non-highlighted text
highlighted_text += text[prev_end:start]
# Append highlighted text with index label
highlighted_text += (f'<span style="background-color:{color}; '
f'border-radius:3px; font-size:14px; display:inline-block;">'
f'{index_label} {text[start:end]}</span>')
prev_end = end
# Append remaining text
highlighted_text += text[prev_end:]
return highlighted_text
# Apply highlighting to both paragraphs using the global MATCH_PAIRS
highlighted_text1 = apply_highlight(text1, match_pairs, "start1", "end1", "index", pair_colors)
highlighted_text2 = apply_highlight(text2, match_pairs, "start2", "end2", "index", pair_colors)
return highlighted_text1, highlighted_text2
if __name__ == '__main__':
# Create Gradio Interface
text1 = ""
with gr.Blocks() as demo:
gr.Markdown("### Highlight Matching Parts Between Two Paragraphs")
text1_input = gr.Textbox(
label="Paragraph 1",
lines=5,
value="The field of deep learning is advancing rapidly. Modern neural networks are improving AI research significantly."
)
text2_input = gr.Textbox(
label="Paragraph 2",
lines=5,
value="Advances in deep learning have led to breakthroughs in AI research. Neural networks are at the core of these innovations"
)
output1 = gr.HTML()
output2 = gr.HTML()
submit_button = gr.Button("Highlight Matches")
submit_button.click(
fn=highlight_pairs,
inputs=[text1_input, text2_input],
outputs=[output1, output2]
)
# Launch the Gradio app
demo.launch()
|