Spaces:
Sleeping
Sleeping
File size: 3,195 Bytes
e58707f b73a4fc e58707f 00b1038 e58707f b73a4fc e58707f 00b1038 b73a4fc 00b1038 b73a4fc e58707f 00b1038 b73a4fc e58707f b73a4fc e58707f b73a4fc e58707f 00b1038 b73a4fc e58707f b73a4fc e58707f b73a4fc e58707f b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc e58707f b73a4fc e58707f b73a4fc e58707f b73a4fc e58707f |
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 |
from pandas import DataFrame
from src.application.config import WORD_BREAK
from src.application.image.image import ImageDetector
from src.application.text.helper import replace_leading_spaces
from src.application.text.text import TextDetector
def create_ordinary_user_table(
aligned_sentences_df: DataFrame,
text: TextDetector,
image: ImageDetector,
) -> str:
"""
Creates an HTML table comparing input news with source news
for ordinary users.
Args:
aligned_sentences_df (DataFrame): Aligned sentence data.
text (TextDetector): Text comparison data.
image (ImageDetector): Image comparison data.
Returns:
A string representing the HTML table.
"""
rows = []
if image.input is not None:
rows.append(format_image_ordinary_user_row(image))
if text.input is not None:
rows.append(format_text_ordinary_user_row(aligned_sentences_df, text))
table = "\n".join(rows)
return f"""
<h5>Comparison between input news and source news:</h5>
<table border="1" style="width:100%; text-align:left;">
<col style="width: 340px;">
<col style="width: 30px;">
<col style="width: 75px;">
<thead>
<tr>
<th>Input news</th>
<th>Forensic</th>
<th>Originality</th>
</tr>
</thead>
<tbody>
{table}
</tbody>
</table>
"""
def format_text_ordinary_user_row(
aligned_sentences_df,
text,
) -> str:
"""
Formats a row for the text in the ordinary user table.
Args:
aligned_sentences_df (DataFrame): Aligned sentence data.
text (TextDetector): Text comparison data.
Returns:
A string representing the HTML table row for the text.
"""
input_sentences = ""
source_text_html = ""
urls = []
for _, row in aligned_sentences_df.iterrows():
if row["input"] is None:
continue
input_sentences += replace_leading_spaces(row["input"]) + "<br>"
url = row["url"]
if url not in urls:
urls.append(url)
source_text_html += f"""<a href="{url}">{url}</a><br>"""
return f"""
<tr>
<td>{input_sentences}</td>
<td>{text.prediction_label[0]}<br>
({text.prediction_score[0] * 100:.2f}%)</td>
<td style="{WORD_BREAK}";>{source_text_html}</td>
</tr>
"""
def format_image_ordinary_user_row(image: ImageDetector) -> str:
"""
Formats a row for the image in the ordinary user table.
Args:
image: Image comparison data.
Returns:
A string representing the HTML table row for the image.
"""
if image.input is None:
return ""
# Put image, label, and score into html tag
if image.referent_url is not None or image.referent_url != "":
source_image_html = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
else:
source_image_html = ""
return f"""
<tr>
<td>input image</td>
<td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
<td style="{WORD_BREAK}";>{source_image_html}</td>
</tr>
"""
|