Spaces:
Running
Running
File size: 8,038 Bytes
35be45b 5b09dd5 35be45b 9914aa0 35be45b 9914aa0 35be45b 9914aa0 35be45b 9914aa0 35be45b 9914aa0 35be45b 6ce8021 35be45b 9914aa0 35be45b 9914aa0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
import gradio as gr
import markdown
import requests
example_dpo_datasets = [
"mlabonne/orpo-dpo-mix-40k",
"argilla/ultrafeedback-binarized-preferences-cleaned",
"argilla/Capybara-Preferences",
]
general_examples = ["davanstrien/cosmochat", "HuggingFaceH4/no_robots"]
datasets_examples = example_dpo_datasets + general_examples
def create_chat_html(messages, dataset_id, offset, compare_mode=False, column=""):
chat_html = ""
for turn_number, i in enumerate(range(0, len(messages), 2), start=1):
user_message = messages[i]
system_message = messages[i + 1] if i + 1 < len(messages) else None
user_role = user_message["role"]
user_content = user_message["content"]
user_content_html = markdown.markdown(user_content)
user_content_length = len(user_content)
user_html = (
'<div class="user-message" style="justify-content: right;">'
+ '<div class="message-content">'
)
user_html += (
f"<strong>Turn {turn_number} - {user_role.capitalize()}:</strong><br>"
)
user_html += f"<em>Length: {user_content_length} characters</em><br><br>"
user_html += f"{user_content_html}"
user_html += "</div></div>"
chat_html += user_html
if system_message:
system_role = system_message["role"]
system_content = system_message["content"]
system_content_html = markdown.markdown(system_content)
system_content_length = len(system_content)
system_html = (
'<div class="system-message" style="justify-content: left;">'
+ '<div class="message-content">'
)
system_html += f"<strong>{system_role.capitalize()}:</strong><br>"
system_html += (
f"<em>Length: {system_content_length} characters</em><br><br>"
)
system_html += f"{system_content_html}"
system_html += "</div></div>"
chat_html += system_html
if compare_mode:
chat_html = f'<div class="column {column}">{chat_html}</div>'
style = """
<style>
.user-message, .system-message {
display: flex;
margin: 10px;
}
.user-message .message-content {
background-color: #c2e3f7;
color: #000000;
}
.system-message .message-content {
background-color: #f5f5f5;
color: #000000;
}
.message-content {
padding: 10px;
border-radius: 10px;
max-width: 70%;
word-wrap: break-word;
}
.container {
display: flex;
justify-content: space-between;
}
.column {
width: 48%;
}
</style>
"""
dataset_url = f"https://huggingface.co/datasets/{dataset_id}/viewer/default/train?row={offset}"
dataset_link = f"[View dataset row]({dataset_url})"
return dataset_link, style + chat_html
def fetch_data(
dataset_id, chosen_column, rejected_column, current_offset, direction, compare_mode
):
change = 1 if direction == "Next" else -1
new_offset = max(0, current_offset + change)
base_url = f"https://datasets-server.huggingface.co/rows?dataset={dataset_id}&config=default&split=train&offset={new_offset}&length=1"
response = requests.get(base_url)
if response.status_code != 200:
return "", "Failed to fetch data", new_offset
data = response.json()
if compare_mode:
if chosen_column and rejected_column:
chosen_messages = data["rows"][0]["row"].get(chosen_column, [])
rejected_messages = data["rows"][0]["row"].get(rejected_column, [])
chosen_link, chosen_html = create_chat_html(
chosen_messages,
dataset_id,
new_offset,
compare_mode=True,
column="chosen",
)
rejected_link, rejected_html = create_chat_html(
rejected_messages,
dataset_id,
new_offset,
compare_mode=True,
column="rejected",
)
chat_html = f'<div class="container">{chosen_html}{rejected_html}</div>'
else:
return (
"",
"Please provide both chosen and rejected columns for comparison",
new_offset,
)
else:
if chosen_column:
messages = data["rows"][0]["row"].get(chosen_column, [])
else:
for key, value in data["rows"][0]["row"].items():
if (
isinstance(value, list)
and len(value) > 0
and isinstance(value[0], dict)
and "role" in value[0]
):
messages = value
break
else:
return "", "No suitable chat column found", new_offset
_, chat_html = create_chat_html(messages, dataset_id, new_offset)
dataset_url = f"https://huggingface.co/datasets/{dataset_id}/viewer/default/train?row={new_offset}"
dataset_link = f"[View dataset row]({dataset_url})"
return dataset_link, chat_html, new_offset
def update_column_names(compare_mode):
return ("chosen", "rejected") if compare_mode else ("", "")
with gr.Blocks() as demo:
with gr.Row():
gr.HTML(
"<h1 style='text-align: center;'>📖 Chat Column Viewer 📖</h1>"
)
gr.HTML(
"<div style='text-align: center;'><em>✨ Explore ChatML formatted data via the datasets viewer API ✨</em></div>"
)
gr.Markdown(
"This app allows you to view chat data from a Hugging Face dataset via the datasets viewer API. ChatML formatted data consists of messages formatted as lists of dictionaries, where each dictionary represents a message with a 'role' (e.g., 'user' or 'assistant') and 'content'. This is a very basic demo built in less than 30 minutes but it hopefully gives you an idea of the kinds of things you can build with the datasets viewer. You can get started building your own apps by going to the datasets viewer documentation [here](https://huggingface.co/docs/datasets-server/index)."
)
with gr.Row():
dataset_id = gr.Dropdown(
datasets_examples,
label="Dataset ID",
allow_custom_value=True,
)
chosen_column = gr.Textbox(
label="Chosen Column",
placeholder="Column containing chosen chat data",
)
rejected_column = gr.Textbox(
label="Rejected Column",
placeholder="Column containing rejected chat data",
)
compare_mode = gr.Checkbox(label="Compare chosen and rejected chats")
current_offset = gr.State(value=0)
with gr.Row():
back_button = gr.Button("Back")
next_button = gr.Button("Next")
dataset_link = gr.Markdown()
output_html = gr.HTML()
compare_mode.change(
fn=update_column_names,
inputs=compare_mode,
outputs=[chosen_column, rejected_column],
)
back_button.click(
lambda data, chosen, rejected, offset, compare: fetch_data(
data, chosen, rejected, offset, "Back", compare
),
inputs=[
dataset_id,
chosen_column,
rejected_column,
current_offset,
compare_mode,
],
outputs=[dataset_link, output_html, current_offset],
)
next_button.click(
lambda data, chosen, rejected, offset, compare: fetch_data(
data, chosen, rejected, offset, "Next", compare
),
inputs=[
dataset_id,
chosen_column,
rejected_column,
current_offset,
compare_mode,
],
outputs=[dataset_link, output_html, current_offset],
)
demo.launch(debug=True)
|