import os
import json
def read_jsonl(file_path):
"""Read a JSONL file and return a dictionary."""
data = {}
with open(file_path, 'r') as file:
for line in file:
item = json.loads(line.strip())
image_path = item.get('image')
text = item.get('text')
if image_path and text:
data[image_path] = text
return data
def get_text_from_jsonl(file_path):
texts = []
with open(file_path, 'r') as f:
for line in f:
# Parse the JSON object
data = json.loads(line.strip())
# Access the "text" field and append to list
if "text" in data:
texts.append(data["text"])
return texts
def generate_html(image_dir, question_file, answer_file, output_file):
# Load the JSONL files
questions = read_jsonl(question_file)
answers = get_text_from_jsonl(answer_file)
# Start the HTML string
html_content = """
Image Questions and Answers
Image Questions and Answers
| Image |
Question or Prompt |
Ground Truth |
Answer |
"""
# Add table rows for each image and its question or prompt
x = 0
for image_path, question_text in questions.items():
image_filename = os.path.basename(image_path)
ground_truth = os.path.basename(os.path.dirname(image_path))
html_content += f"""
 |
{question_text} |
{ground_truth} |
{answers[x]} |
"""
x+=1
# End the HTML string
html_content += """
"""
# Write the HTML string to the output file
with open(output_file, 'w') as f:
f.write(html_content)
print(f"HTML file '{output_file}' generated successfully.")
# Example usage
image_directory = 'my_code/json/testimages2' # Directory containing images
question_filename = '/mnt/user/myang/LLaVA-2/my_code/json/llavatestquestions2.jsonl' # JSON file with questions
answer_filename = '/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlora2v3.jsonl' # JSON file with answers
output_html_file = '/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlora2v3html.html' # Output HTML file
generate_html(image_directory, question_filename, answer_filename, output_html_file)
# def generate_html(image_dir, question_file, answer_file, output_file):
# # Load the JSON files
# with open(question_file, 'r') as qf:
# questions = json.load(qf)
# with open(answer_file, 'r') as af:
# answers = json.load(af)
# # Start the HTML string
# html_content = """
#
#
#
#
#
# Image Questions and Answers
#
#
#
# Image Questions and Answers
#
#
# | Image |
# Question |
# Answer |
#
# """
# # Add table rows for each image, its question, and answer
# for i in range(len(questions)):
# entry=questions[i]
# image_path = os.path.join("testimages", entry.get('image'))
# question = entry.get('text')
# answer = answers[i].get('text')
# html_content += f"""
#
#  |
# {question} |
# {answer} |
#
# """
# # End the HTML string
# html_content += """
#
#
#
# """
# # Write the HTML string to the output file
# with open(output_file, 'w') as f:
# f.write(html_content)
# print(f"HTML file '{output_file}' generated successfully.")