Spaces:
Running
Running
Parse markdown responses (#4)
Browse files- Added markdown rendering and improving UI (2b408f3cc7285795f4e06f971e6257a2280042cd)
- app.py +20 -9
- requirements.txt +1 -0
- utils.py +9 -8
app.py
CHANGED
@@ -33,39 +33,50 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
33 |
current_html = gr.State(value="")
|
34 |
|
35 |
def load_initial(model, category):
|
|
|
36 |
html = display_model_responses_html(evaluation_data, responses, model, start_index=0, batch_size=5, category=category)
|
37 |
-
|
|
|
|
|
38 |
|
39 |
def load_more(model, index, html_so_far, category):
|
|
|
|
|
40 |
new_html = display_model_responses_html(evaluation_data, responses, model, start_index=index, batch_size=5, category=category)
|
41 |
updated_html = html_so_far + new_html
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
43 |
|
|
|
|
|
44 |
selected_model.change(
|
45 |
load_initial,
|
46 |
inputs=[selected_model, model_category],
|
47 |
-
outputs=[model_output, current_index, current_html]
|
48 |
)
|
49 |
model_category.change(
|
50 |
load_initial,
|
51 |
inputs=[selected_model, model_category],
|
52 |
-
outputs=[model_output, current_index, current_html]
|
53 |
)
|
54 |
|
55 |
demo.load(
|
56 |
load_initial,
|
57 |
inputs=[selected_model, model_category],
|
58 |
-
outputs=[model_output, current_index, current_html]
|
59 |
)
|
60 |
-
|
61 |
-
|
62 |
-
more_button = gr.Button("Load more")
|
63 |
more_button.click(
|
64 |
load_more,
|
65 |
inputs=[selected_model, current_index, current_html, model_category],
|
66 |
-
outputs=[model_output, current_index, current_html]
|
67 |
)
|
68 |
|
|
|
69 |
with gr.Column(visible=False) as example_mode:
|
70 |
category = gr.Dropdown(
|
71 |
choices=list(set(ex["category"] for ex in evaluation_data)),
|
|
|
33 |
current_html = gr.State(value="")
|
34 |
|
35 |
def load_initial(model, category):
|
36 |
+
filtered_data = [ex for ex in evaluation_data if ex["category"] == category]
|
37 |
html = display_model_responses_html(evaluation_data, responses, model, start_index=0, batch_size=5, category=category)
|
38 |
+
has_more = 5 < len(filtered_data)
|
39 |
+
return html, 5, html, gr.update(visible=has_more)
|
40 |
+
|
41 |
|
42 |
def load_more(model, index, html_so_far, category):
|
43 |
+
filtered_data = [ex for ex in evaluation_data if ex["category"] == category]
|
44 |
+
|
45 |
new_html = display_model_responses_html(evaluation_data, responses, model, start_index=index, batch_size=5, category=category)
|
46 |
updated_html = html_so_far + new_html
|
47 |
+
|
48 |
+
new_index = index + 5
|
49 |
+
has_more = new_index < len(filtered_data)
|
50 |
+
|
51 |
+
return updated_html, new_index, updated_html, gr.update(visible=has_more)
|
52 |
+
|
53 |
|
54 |
+
more_button = gr.Button("Load more")
|
55 |
+
|
56 |
selected_model.change(
|
57 |
load_initial,
|
58 |
inputs=[selected_model, model_category],
|
59 |
+
outputs=[model_output, current_index, current_html, more_button]
|
60 |
)
|
61 |
model_category.change(
|
62 |
load_initial,
|
63 |
inputs=[selected_model, model_category],
|
64 |
+
outputs=[model_output, current_index, current_html, more_button]
|
65 |
)
|
66 |
|
67 |
demo.load(
|
68 |
load_initial,
|
69 |
inputs=[selected_model, model_category],
|
70 |
+
outputs=[model_output, current_index, current_html, more_button]
|
71 |
)
|
72 |
+
|
|
|
|
|
73 |
more_button.click(
|
74 |
load_more,
|
75 |
inputs=[selected_model, current_index, current_html, model_category],
|
76 |
+
outputs=[model_output, current_index, current_html, more_button]
|
77 |
)
|
78 |
|
79 |
+
|
80 |
with gr.Column(visible=False) as example_mode:
|
81 |
category = gr.Dropdown(
|
82 |
choices=list(set(ex["category"] for ex in evaluation_data)),
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
markdown
|
utils.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import base64
|
2 |
import time
|
|
|
3 |
|
4 |
from io import BytesIO
|
5 |
|
@@ -13,7 +14,6 @@ def get_examples_by_category(evaluation_data, category):
|
|
13 |
return [ex["id"] for ex in evaluation_data if ex["category"] == category]
|
14 |
|
15 |
def display_model_responses_html(evaluation_data, responses, model, start_index=0, batch_size=5, category=None):
|
16 |
-
start_time = time.time()
|
17 |
html = ""
|
18 |
|
19 |
if category is not None:
|
@@ -24,10 +24,10 @@ def display_model_responses_html(evaluation_data, responses, model, start_index=
|
|
24 |
for ex in filtered_data[start_index:start_index + batch_size]:
|
25 |
image_thumbnail = ex["image_thumbnail"]
|
26 |
image_full_url = ex["image_full_url"]
|
27 |
-
img_id = ex["id"]
|
28 |
prompt = ex["prompt"]
|
29 |
-
response = responses[model].get(ex["id"], "(No response)")
|
30 |
category = ex["category"]
|
|
|
|
|
31 |
|
32 |
html += f"""
|
33 |
<div style="display: flex; margin-bottom: 20px; align-items: flex-start;">
|
@@ -41,15 +41,16 @@ def display_model_responses_html(evaluation_data, responses, model, start_index=
|
|
41 |
<strong style="color: var(--body-text-color);">Category:</strong> {category}</p>
|
42 |
<p style="margin: 0 0 10px; font-size: 16px;">
|
43 |
<strong style="color: var(--body-text-color);">Prompt:</strong> {prompt}</p>
|
44 |
-
<
|
45 |
-
<strong style="color: var(--body-text-color);">Response:</strong>
|
|
|
|
|
|
|
|
|
46 |
</div>
|
47 |
</div>
|
48 |
<hr/>
|
49 |
"""
|
50 |
-
elapsed_time = time.time() - start_time
|
51 |
-
print(f"[TRACE] display_model_responses_html took {elapsed_time:.3f} seconds for {batch_size} items")
|
52 |
-
|
53 |
return html
|
54 |
|
55 |
|
|
|
1 |
import base64
|
2 |
import time
|
3 |
+
import markdown
|
4 |
|
5 |
from io import BytesIO
|
6 |
|
|
|
14 |
return [ex["id"] for ex in evaluation_data if ex["category"] == category]
|
15 |
|
16 |
def display_model_responses_html(evaluation_data, responses, model, start_index=0, batch_size=5, category=None):
|
|
|
17 |
html = ""
|
18 |
|
19 |
if category is not None:
|
|
|
24 |
for ex in filtered_data[start_index:start_index + batch_size]:
|
25 |
image_thumbnail = ex["image_thumbnail"]
|
26 |
image_full_url = ex["image_full_url"]
|
|
|
27 |
prompt = ex["prompt"]
|
|
|
28 |
category = ex["category"]
|
29 |
+
raw_response = responses[model].get(ex["id"], "(No response)")
|
30 |
+
response_html = markdown.markdown(raw_response, extensions=['fenced_code', 'codehilite'])
|
31 |
|
32 |
html += f"""
|
33 |
<div style="display: flex; margin-bottom: 20px; align-items: flex-start;">
|
|
|
41 |
<strong style="color: var(--body-text-color);">Category:</strong> {category}</p>
|
42 |
<p style="margin: 0 0 10px; font-size: 16px;">
|
43 |
<strong style="color: var(--body-text-color);">Prompt:</strong> {prompt}</p>
|
44 |
+
<div style="margin-top: 10px;">
|
45 |
+
<strong style="color: var(--body-text-color);">Response:</strong>
|
46 |
+
<div style="margin-top: 4px; font-size: 15px; color: var(--body-text-color);">
|
47 |
+
{response_html}
|
48 |
+
</div>
|
49 |
+
</div>
|
50 |
</div>
|
51 |
</div>
|
52 |
<hr/>
|
53 |
"""
|
|
|
|
|
|
|
54 |
return html
|
55 |
|
56 |
|