aliasgerovs commited on
Commit
b105c66
2 Parent(s): 1c3ac85 38d87ea

Merge branch 'main' into demo

Browse files
Files changed (2) hide show
  1. app.py +0 -7
  2. highlighter.py +25 -10
app.py CHANGED
@@ -307,13 +307,6 @@ with gr.Blocks() as demo:
307
  api_name="bc_highlighter",
308
  )
309
 
310
- # quillbot_highlighter.click(
311
- # fn=analyze_and_highlight,
312
- # inputs=[input_text],
313
- # outputs=[highlighter_html],
314
- # api_name="quillbot_highlighter",
315
- # )
316
-
317
  date_from = ""
318
  date_to = ""
319
 
 
307
  api_name="bc_highlighter",
308
  )
309
 
 
 
 
 
 
 
 
310
  date_from = ""
311
  date_to = ""
312
 
highlighter.py CHANGED
@@ -26,24 +26,39 @@ def explainer(text, model_type):
26
 
27
 
28
  def analyze_and_highlight(text, model_type):
 
29
  highlighted_text = ""
30
  sentences_weights, _ = explainer(text, model_type)
31
- min_weight = min(sentences_weights.values())
32
- max_weight = max(sentences_weights.values())
33
 
34
- for sentence, weight in sentences_weights.items():
35
- normalized_weight = (weight - min_weight) / (max_weight - min_weight)
36
- if weight >= 0:
37
- color = f"rgba(255, {255 * (1 - normalized_weight)}, {255 * (1 - normalized_weight)}, 1)"
38
- else:
39
- color = (
40
- f"rgba({255 * normalized_weight}, 255, {255 * normalized_weight}, 1)"
41
- )
42
 
 
43
  sentence = sentence.strip()
44
  if not sentence:
45
  continue
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  highlighted_sentence = (
48
  f'<span style="background-color: {color}; color: black;">{sentence}</span> '
49
  )
 
26
 
27
 
28
  def analyze_and_highlight(text, model_type):
29
+
30
  highlighted_text = ""
31
  sentences_weights, _ = explainer(text, model_type)
32
+ positive_weights = [weight for weight in sentences_weights.values() if weight >= 0]
33
+ negative_weights = [weight for weight in sentences_weights.values() if weight < 0]
34
 
35
+ smoothing_factor = 0.001 # we do this cos to avoid all white colors
36
+ min_positive_weight = min(positive_weights) if positive_weights else 0
37
+ max_positive_weight = max(positive_weights) if positive_weights else 0
38
+ min_negative_weight = min(negative_weights) if negative_weights else 0
39
+ max_negative_weight = max(negative_weights) if negative_weights else 0
40
+
41
+ max_positive_weight += smoothing_factor
42
+ min_negative_weight -= smoothing_factor
43
 
44
+ for sentence, weight in sentences_weights.items():
45
  sentence = sentence.strip()
46
  if not sentence:
47
  continue
48
 
49
+ if weight >= 0 and max_positive_weight != min_positive_weight:
50
+ normalized_weight = (weight - min_positive_weight + smoothing_factor) / (
51
+ max_positive_weight - min_positive_weight
52
+ )
53
+ color = f"rgb(255, {int(255 * (1 - normalized_weight))}, {int(255 * (1 - normalized_weight))})"
54
+ elif weight < 0 and min_negative_weight != max_negative_weight:
55
+ normalized_weight = (weight - max_negative_weight - smoothing_factor) / (
56
+ min_negative_weight - max_negative_weight
57
+ )
58
+ color = f"rgb({int(255 * (1 - normalized_weight))}, 255, {int(255 * (1 - normalized_weight))})"
59
+ else:
60
+ color = "rgb(255, 255, 255)" # when no range
61
+
62
  highlighted_sentence = (
63
  f'<span style="background-color: {color}; color: black;">{sentence}</span> '
64
  )