news_verification / src /application /formatting_ordinary_user.py
pmkhanh7890's picture
refactor code and fix bugs
b73a4fc
raw
history blame
2.43 kB
from src.application.config import WORD_BREAK
def create_ordinary_user_table(self):
rows = []
rows.append(self.format_image_ordinary_user_row())
rows.append(self.format_text_ordinary_user_row())
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(self):
input_sentences = ""
source_text_urls = ""
urls = []
for _, row in self.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_urls += f"""<a href="{url}">{url}</a><br>"""
return f"""
<tr>
<td>{input_sentences}</td>
<td>{self.text_prediction_label[0]}<br>
({self.text_prediction_score[0] * 100:.2f}%)</td>
<td style="{WORD_BREAK}";>{source_text_urls}</td>
</tr>
"""
def format_image_ordinary_user_row(
image_referent_url: str,
image_prediction_label: str,
image_prediction_score: float,
):
"""
Formats an HTML table row for ordinary users,
displaying image analysis results.
Args:
image_referent_url (str): The URL of the referenced image.
image_prediction_label (str): The predicted label for the image.
image_prediction_score (float): The prediction score for the image.
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_url = f"""<a href="{image_referent_url}">{image_referent_url}</a>""" # noqa: E501
else:
source_image_url = ""
return f"""
<tr>
<td>input image</td>
<td>{image_prediction_label}<br>({image_prediction_score:.2f}%)</td>
<td style="{WORD_BREAK}";>{source_image_url}</td>
</tr>
"""