pmkhanh7890 commited on
Commit
5842223
·
1 Parent(s): 00b1038

fix bug of showing forensic

Browse files
src/application/formatting_fact_checker.py CHANGED
@@ -5,6 +5,7 @@ from src.application.formatting import (
5
  color_text,
6
  format_entity_count,
7
  )
 
8
  from src.application.image.image import ImageDetector
9
  from src.application.text.entity import apply_highlight
10
  from src.application.text.helper import (
@@ -238,15 +239,22 @@ def format_image_fact_checker_row(image: ImageDetector):
238
  return ""
239
 
240
  if image.referent_url is not None or image.referent_url != "":
241
- source_image = f"""<img src="{image.referent_url}" width="100" height="150">""" # noqa: E501
 
 
 
 
 
 
242
  source_image_url = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
 
243
  else:
244
  source_image = "Image not found"
245
  source_image_url = ""
246
 
247
  return f"""
248
  <tr>
249
- <td>input image</td>
250
  <td>{source_image}</td>
251
  <td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
252
  <td style="{WORD_BREAK}";>{source_image_url}</td>
 
5
  color_text,
6
  format_entity_count,
7
  )
8
+ from src.application.image.helper import encode_image
9
  from src.application.image.image import ImageDetector
10
  from src.application.text.entity import apply_highlight
11
  from src.application.text.helper import (
 
239
  return ""
240
 
241
  if image.referent_url is not None or image.referent_url != "":
242
+ if "http" in image.input:
243
+ input_image = (
244
+ f"""<a href="{image.input}">{image.input}</a>""" # noqa: E501
245
+ )
246
+ else:
247
+ base64_image = encode_image(image.input)
248
+ input_image = f"""<img src="data:image/jpeg;base64,{base64_image}" width="100" height="150">""" # noqa: E501
249
  source_image_url = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
250
+ source_image = f"""<img src="{image.referent_url}" width="100" height="150">""" # noqa: E501
251
  else:
252
  source_image = "Image not found"
253
  source_image_url = ""
254
 
255
  return f"""
256
  <tr>
257
+ <td>{input_image}</td>
258
  <td>{source_image}</td>
259
  <td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
260
  <td style="{WORD_BREAK}";>{source_image_url}</td>
src/application/formatting_governor.py CHANGED
@@ -5,6 +5,7 @@ from src.application.formatting import (
5
  color_text,
6
  format_entity_count,
7
  )
 
8
  from src.application.image.image import ImageDetector
9
  from src.application.text.entity import apply_highlight
10
  from src.application.text.helper import (
@@ -155,20 +156,27 @@ def format_text_governor_row(text):
155
  """
156
 
157
 
158
- def format_image_governor_row(image):
159
  if image.input is None:
160
  return ""
161
 
162
  if image.referent_url is not None or image.referent_url != "":
163
- source_image = f"""<img src="{image.referent_url}" width="100" height="150">""" # noqa: E501
 
 
 
 
 
 
164
  source_image_url = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
 
165
  else:
166
  source_image = "Image not found"
167
  source_image_url = ""
168
 
169
  return f"""
170
  <tr>
171
- <td>input image</td>
172
  <td>{source_image}</td>
173
  <td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
174
  <td style="{WORD_BREAK}";>{source_image_url}</td>
 
5
  color_text,
6
  format_entity_count,
7
  )
8
+ from src.application.image.helper import encode_image
9
  from src.application.image.image import ImageDetector
10
  from src.application.text.entity import apply_highlight
11
  from src.application.text.helper import (
 
156
  """
157
 
158
 
159
+ def format_image_governor_row(image: ImageDetector):
160
  if image.input is None:
161
  return ""
162
 
163
  if image.referent_url is not None or image.referent_url != "":
164
+ if "http" in image.input:
165
+ input_image = (
166
+ f"""<a href="{image.input}">{image.input}</a>""" # noqa: E501
167
+ )
168
+ else:
169
+ base64_image = encode_image(image.input)
170
+ input_image = f"""<img src="data:image/jpeg;base64,{base64_image}" width="100" height="150">""" # noqa: E501
171
  source_image_url = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
172
+ source_image = f"""<img src="{image.referent_url}" width="100" height="150">""" # noqa: E501
173
  else:
174
  source_image = "Image not found"
175
  source_image_url = ""
176
 
177
  return f"""
178
  <tr>
179
+ <td>{input_image}</td>
180
  <td>{source_image}</td>
181
  <td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
182
  <td style="{WORD_BREAK}";>{source_image_url}</td>
src/application/formatting_ordinary_user.py CHANGED
@@ -1,6 +1,7 @@
1
  from pandas import DataFrame
2
 
3
  from src.application.config import WORD_BREAK
 
4
  from src.application.image.image import ImageDetector
5
  from src.application.text.helper import replace_leading_spaces
6
  from src.application.text.text import TextDetector
@@ -105,13 +106,20 @@ def format_image_ordinary_user_row(image: ImageDetector) -> str:
105
 
106
  # Put image, label, and score into html tag
107
  if image.referent_url is not None or image.referent_url != "":
 
 
 
 
 
 
 
108
  source_image_html = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
109
  else:
110
  source_image_html = ""
111
 
112
  return f"""
113
  <tr>
114
- <td>input image</td>
115
  <td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
116
  <td style="{WORD_BREAK}";>{source_image_html}</td>
117
  </tr>
 
1
  from pandas import DataFrame
2
 
3
  from src.application.config import WORD_BREAK
4
+ from src.application.image.helper import encode_image
5
  from src.application.image.image import ImageDetector
6
  from src.application.text.helper import replace_leading_spaces
7
  from src.application.text.text import TextDetector
 
106
 
107
  # Put image, label, and score into html tag
108
  if image.referent_url is not None or image.referent_url != "":
109
+ if "http" in image.input:
110
+ input_image = (
111
+ f"""<a href="{image.input}">{image.input}</a>""" # noqa: E501
112
+ )
113
+ else:
114
+ base64_image = encode_image(image.input)
115
+ input_image = f"""<img src="data:image/jpeg;base64,{base64_image}" width="100" height="150">""" # noqa: E501
116
  source_image_html = f"""<a href="{image.referent_url}">{image.referent_url}</a>""" # noqa: E501
117
  else:
118
  source_image_html = ""
119
 
120
  return f"""
121
  <tr>
122
+ <td>{input_image}</td>
123
  <td>{image.prediction_label}<br>({image.prediction_score:.2f}%)</td>
124
  <td style="{WORD_BREAK}";>{source_image_html}</td>
125
  </tr>
src/application/image/helper.py CHANGED
@@ -1,12 +1,24 @@
1
  import base64
2
 
3
 
4
- def encode_image(image_path):
5
- with open(image_path, "rb") as img_file:
6
- return base64.b64encode(img_file.read()).decode()
7
 
 
 
8
 
9
- image_base64 = encode_image(
10
- "/content/ai-generated-picture-of-a-tiger-walking-in-the-forest-photo.jpg",
11
- )
12
- html_code = f'<img src="data:image/jpeg;base64,{image_base64}" width="300">'
 
 
 
 
 
 
 
 
 
 
 
1
  import base64
2
 
3
 
4
+ def encode_image(image_path: str) -> str:
5
+ """
6
+ Encodes an image file into a Base64 string.
7
 
8
+ Args:
9
+ image_path (str): The path to the image file.
10
 
11
+ Returns:
12
+ str: The Base64 encoded string representation of the image.
13
+
14
+ Raises:
15
+ FileNotFoundError: If the specified image file does not exist.
16
+ IOError: If there is an error reading the image file.
17
+ """
18
+ try:
19
+ with open(image_path, "rb") as img_file:
20
+ return base64.b64encode(img_file.read()).decode()
21
+ except FileNotFoundError:
22
+ raise FileNotFoundError(f"Image file not found at: {image_path}")
23
+ except OSError as e:
24
+ raise OSError(f"Error reading image file: {e}")