deepfaketar / python /generatehtml.py
blorg469's picture
Upload folder using huggingface_hub
90cd92a verified
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 = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Questions and Answers</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
img {
max-width: 200px;
height: auto;
}
</style>
</head>
<body>
<h1>Image Questions and Answers</h1>
<table>
<tr>
<th>Image</th>
<th>Question or Prompt</th>
<th>Ground Truth</th>
<th>Answer</th>
</tr>
"""
# 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"""
<tr>
<td><img src="testimages2/{image_filename}" alt="{image_filename}"></td>
<td>{question_text}</td>
<td>{ground_truth}</td>
<td>{answers[x]}</td>
</tr>
"""
x+=1
# End the HTML string
html_content += """
</table>
</body>
</html>
"""
# 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 = """
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8">
# <meta name="viewport" content="width=device-width, initial-scale=1.0">
# <title>Image Questions and Answers</title>
# <style>
# table {
# width: 100%;
# border-collapse: collapse;
# }
# th, td {
# border: 1px solid black;
# padding: 8px;
# text-align: left;
# }
# th {
# background-color: #f2f2f2;
# }
# </style>
# </head>
# <body>
# <h1>Image Questions and Answers</h1>
# <table>
# <tr>
# <th>Image</th>
# <th>Question</th>
# <th>Answer</th>
# </tr>
# """
# # 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"""
# <tr>
# <td><img src="{image_path}" alt="{entry.get('image')}" style="max-width:200px;"></td>
# <td>{question}</td>
# <td>{answer}</td>
# </tr>
# """
# # End the HTML string
# html_content += """
# </table>
# </body>
# </html>
# """
# # 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.")