Spaces:
Sleeping
Sleeping
Petr Tsvetkov
commited on
Commit
·
fddb192
1
Parent(s):
7fbd3e2
Create update function for diff view using diff2htmlUI js library instead of iframe
Browse files
app.py
CHANGED
|
@@ -19,38 +19,48 @@ n_samples = len(dataset)
|
|
| 19 |
saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
|
| 20 |
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
repo_url = f"https://github.com/{repo}/
|
| 24 |
return repo_url
|
| 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 |
def update_commit_view(sample_ind):
|
|
@@ -58,15 +68,16 @@ def update_commit_view(sample_ind):
|
|
| 58 |
return None
|
| 59 |
|
| 60 |
record = dataset[sample_ind]
|
| 61 |
-
|
| 62 |
|
| 63 |
-
github_link_md = f"[See the commit on GitHub]({github_link})"
|
| 64 |
-
|
|
|
|
| 65 |
commit_msg = record['message']
|
| 66 |
repo_val = record['repo']
|
| 67 |
hash_val = record['hash']
|
| 68 |
diff_loaded_timestamp = datetime.now().isoformat()
|
| 69 |
-
return
|
| 70 |
|
| 71 |
|
| 72 |
def next_sample(current_sample_ind, shuffled_idx):
|
|
@@ -78,7 +89,16 @@ def next_sample(current_sample_ind, shuffled_idx):
|
|
| 78 |
return (current_sample_ind,) + updated_view
|
| 79 |
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
|
| 83 |
hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
|
| 84 |
shuffled_idx_val = gr.JSON(visible=False)
|
|
@@ -96,7 +116,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 96 |
skip_btn = gr.Button("Skip the current sample")
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column(scale=2):
|
| 99 |
-
github_link = gr.Markdown()
|
| 100 |
diff_view = gr.HTML()
|
| 101 |
with gr.Column(scale=1):
|
| 102 |
commit_msg = gr.Textbox(label="Commit message",
|
|
@@ -136,7 +155,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 136 |
label='submitted_ts')
|
| 137 |
|
| 138 |
commit_view = [
|
| 139 |
-
github_link,
|
| 140 |
diff_view,
|
| 141 |
commit_msg,
|
| 142 |
repo_val,
|
|
@@ -155,7 +173,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 155 |
format_feedback
|
| 156 |
]
|
| 157 |
|
| 158 |
-
saver.setup([current_sample_sld] + feedback_form
|
| 159 |
|
| 160 |
skip_btn.click(next_sample, inputs=[current_sample_sld, shuffled_idx_val],
|
| 161 |
outputs=[current_sample_sld] + commit_view)
|
|
@@ -177,6 +195,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 177 |
return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
|
| 178 |
|
| 179 |
|
| 180 |
-
|
| 181 |
|
| 182 |
-
|
|
|
|
| 19 |
saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
|
| 20 |
|
| 21 |
|
| 22 |
+
def get_github_api_url(repo, hash):
|
| 23 |
+
repo_url = f"https://api.github.com/repos/{repo}/commits/{hash}"
|
| 24 |
return repo_url
|
| 25 |
|
| 26 |
|
| 27 |
+
DIFF_VIEW_UPDATE_JS_FN = """
|
| 28 |
+
<script>
|
| 29 |
+
function updateDiffView() {
|
| 30 |
+
var github_api_url = document.getElementById('diff-view').getAttribute("github-api-url");
|
| 31 |
+
var xmlHttp = new XMLHttpRequest();
|
| 32 |
+
xmlHttp.onreadystatechange = function() {{
|
| 33 |
+
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
|
| 34 |
+
var diff = xmlHttp.responseText;
|
| 35 |
+
console.log(diff);
|
| 36 |
+
var targetElement = document.getElementById('diff-view');
|
| 37 |
+
var configuration = {
|
| 38 |
+
drawFileList: true,
|
| 39 |
+
matching: 'lines',
|
| 40 |
+
highlight: true
|
| 41 |
+
};
|
| 42 |
+
var diff2htmlUi = new Diff2HtmlUI(targetElement, diff, configuration);
|
| 43 |
+
diff2htmlUi.draw();
|
| 44 |
+
}}
|
| 45 |
+
xmlHttp.open("GET", github_api_url, true);
|
| 46 |
+
xmlHttp.setRequestHeader("Accept", "application/vnd.github.v3.diff");
|
| 47 |
+
xmlHttp.send();
|
| 48 |
+
}
|
| 49 |
+
</script>
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def get_diff2html_view(github_api_url):
|
| 54 |
+
html = f"""
|
| 55 |
+
<div style='width:100%; height:720px; overflow:auto'>
|
| 56 |
+
<div
|
| 57 |
+
id='diff-view'
|
| 58 |
+
github-api-url="{github_api_url}"
|
| 59 |
+
></div>
|
| 60 |
+
</div>
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
return html
|
| 64 |
|
| 65 |
|
| 66 |
def update_commit_view(sample_ind):
|
|
|
|
| 68 |
return None
|
| 69 |
|
| 70 |
record = dataset[sample_ind]
|
| 71 |
+
github_api_url = get_github_api_url(record['repo'], record['hash'])
|
| 72 |
|
| 73 |
+
# github_link_md = f"[See the commit on GitHub]({github_link})"
|
| 74 |
+
|
| 75 |
+
diff_view = get_diff2html_view(github_api_url)
|
| 76 |
commit_msg = record['message']
|
| 77 |
repo_val = record['repo']
|
| 78 |
hash_val = record['hash']
|
| 79 |
diff_loaded_timestamp = datetime.now().isoformat()
|
| 80 |
+
return diff_view, commit_msg, repo_val, hash_val, diff_loaded_timestamp
|
| 81 |
|
| 82 |
|
| 83 |
def next_sample(current_sample_ind, shuffled_idx):
|
|
|
|
| 89 |
return (current_sample_ind,) + updated_view
|
| 90 |
|
| 91 |
|
| 92 |
+
DIFF2HTML_IMPORTS = """
|
| 93 |
+
<!-- Stylesheet -->
|
| 94 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" />
|
| 95 |
+
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />
|
| 96 |
+
|
| 97 |
+
<!-- Javascripts -->
|
| 98 |
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
with gr.Blocks(theme=gr.themes.Soft(), head=DIFF2HTML_IMPORTS + DIFF_VIEW_UPDATE_JS_FN) as application:
|
| 102 |
repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
|
| 103 |
hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
|
| 104 |
shuffled_idx_val = gr.JSON(visible=False)
|
|
|
|
| 116 |
skip_btn = gr.Button("Skip the current sample")
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column(scale=2):
|
|
|
|
| 119 |
diff_view = gr.HTML()
|
| 120 |
with gr.Column(scale=1):
|
| 121 |
commit_msg = gr.Textbox(label="Commit message",
|
|
|
|
| 155 |
label='submitted_ts')
|
| 156 |
|
| 157 |
commit_view = [
|
|
|
|
| 158 |
diff_view,
|
| 159 |
commit_msg,
|
| 160 |
repo_val,
|
|
|
|
| 173 |
format_feedback
|
| 174 |
]
|
| 175 |
|
| 176 |
+
saver.setup([current_sample_sld] + feedback_form, "feedback")
|
| 177 |
|
| 178 |
skip_btn.click(next_sample, inputs=[current_sample_sld, shuffled_idx_val],
|
| 179 |
outputs=[current_sample_sld] + commit_view)
|
|
|
|
| 195 |
return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
|
| 196 |
|
| 197 |
|
| 198 |
+
application.load(init_session, inputs=[current_sample_sld], outputs=[session_val, shuffled_idx_val] + commit_view)
|
| 199 |
|
| 200 |
+
application.launch()
|