news_verification / src /application /formatting_ordinary_user.py
pmkhanh7890's picture
refactor
e58707f
raw
history blame
2.44 kB
from pandas import DataFrame
from src.application.config import WORD_BREAK
from src.application.image.image import ImageDetector
from src.application.text.text import TextDetector
def create_ordinary_user_table(
aligned_sentences_df: DataFrame,
text: TextDetector,
image: ImageDetector,
) -> str:
rows = []
rows.append(format_image_ordinary_user_row(image))
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>
<style>
"""
def format_text_ordinary_user_row(
aligned_sentences_df,
text,
) -> str:
input_sentences = ""
source_text_html = ""
urls = []
for _, row in aligned_sentences_df.iterrows():
if row["input"] is None:
continue
input_sentences += row["input"] + "<br><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 an HTML table row for ordinary users,
displaying image analysis results.
Args:
image (ImageDetector): The image to be analyzed.
Returns:
str: An HTML table row string containing the image analysis results.
"""
# 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>
"""