wilwork commited on
Commit
5933aa3
·
verified ·
1 Parent(s): 271e600

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -8,7 +8,7 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
  model.eval()
10
 
11
- # Function to compute relevance score (in logits) and dynamically adjust threshold
12
  def get_relevance_score_and_excerpt(query, paragraph1, paragraph2, paragraph3, threshold_weight):
13
  # Handle empty input for paragraphs
14
  paragraphs = [p for p in [paragraph1, paragraph2, paragraph3] if p.strip()]
@@ -73,13 +73,19 @@ def get_relevance_score_and_excerpt(query, paragraph1, paragraph2, paragraph3, t
73
  # Sort paragraphs by logit (descending)
74
  ranked_paragraphs.sort(key=lambda x: x["logit"], reverse=True)
75
 
76
- # Prepare output
77
- relevance_scores = [round(p["logit"], 4) for p in ranked_paragraphs]
78
- highlighted_texts = [p["highlighted_text"] for p in ranked_paragraphs]
79
 
80
- return "\n".join([f"Relevance Score (Logits): {score}" for score in relevance_scores]), "\n\n".join(highlighted_texts)
 
 
81
 
82
- # Define Gradio interface with a slider for threshold adjustment and ability to add multiple paragraphs
 
 
 
 
83
  interface = gr.Interface(
84
  fn=get_relevance_score_and_excerpt,
85
  inputs=[
@@ -90,8 +96,7 @@ interface = gr.Interface(
90
  gr.Slider(minimum=0.02, maximum=0.5, value=0.1, step=0.01, label="Attention Threshold")
91
  ],
92
  outputs=[
93
- gr.Textbox(label="Relevance Scores (Logits)"),
94
- gr.HTML(label="Highlighted Document Paragraphs")
95
  ],
96
  title="Cross-Encoder Attention Highlighting with Reranking",
97
  description="Adjust the attention threshold to control token highlighting sensitivity. Multiple paragraphs can be added and reranked based on their logits.",
 
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
  model.eval()
10
 
11
+ # Function to compute relevance scores (in logits) and dynamically adjust threshold
12
  def get_relevance_score_and_excerpt(query, paragraph1, paragraph2, paragraph3, threshold_weight):
13
  # Handle empty input for paragraphs
14
  paragraphs = [p for p in [paragraph1, paragraph2, paragraph3] if p.strip()]
 
73
  # Sort paragraphs by logit (descending)
74
  ranked_paragraphs.sort(key=lambda x: x["logit"], reverse=True)
75
 
76
+ # Prepare output: Combine scores and highlighted text in a readable format
77
+ output_html = "<table border='1' style='width:100%; border-collapse: collapse;'>"
78
+ output_html += "<tr><th style='padding: 8px;'>Relevance Score (Logits)</th><th style='padding: 8px;'>Highlighted Paragraph</th></tr>"
79
 
80
+ for item in ranked_paragraphs:
81
+ output_html += f"<tr><td style='padding: 8px; text-align: center;'>{round(item['logit'], 4)}</td>"
82
+ output_html += f"<td style='padding: 8px;'>{item['highlighted_text']}</td></tr>"
83
 
84
+ output_html += "</table>"
85
+
86
+ return output_html
87
+
88
+ # Define Gradio interface with a slider for threshold adjustment and multiple paragraphs input
89
  interface = gr.Interface(
90
  fn=get_relevance_score_and_excerpt,
91
  inputs=[
 
96
  gr.Slider(minimum=0.02, maximum=0.5, value=0.1, step=0.01, label="Attention Threshold")
97
  ],
98
  outputs=[
99
+ gr.HTML(label="Ranked Paragraphs")
 
100
  ],
101
  title="Cross-Encoder Attention Highlighting with Reranking",
102
  description="Adjust the attention threshold to control token highlighting sensitivity. Multiple paragraphs can be added and reranked based on their logits.",