Spaces:
Sleeping
Sleeping
lancewilhelm
commited on
Commit
•
cb8ee36
1
Parent(s):
dd891d3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import json
|
5 |
+
from datetime import datetime
|
6 |
+
from pathlib import Path
|
7 |
+
from uuid import uuid4
|
8 |
+
|
9 |
+
from huggingface_hub import CommitScheduler
|
10 |
+
|
11 |
+
# Global variable to store the DataFrame
|
12 |
+
df = pd.read_csv('./feedback_results.csv')
|
13 |
+
df = df[['feedback','feedback_local','feedback_web']].rename(columns={'feedback':'none','feedback_local':'local','feedback_web':'web'})
|
14 |
+
col_names = list(df.columns)
|
15 |
+
|
16 |
+
# Generate intial random feedback to show
|
17 |
+
sample = {}
|
18 |
+
def get_random_feedback():
|
19 |
+
x = df.sample(1, replace=True, axis=0)
|
20 |
+
sample['index'] = x.index[0]
|
21 |
+
x = x.sample(2, replace=False, axis=1)
|
22 |
+
sample['a_method'] = x.columns[0]
|
23 |
+
sample['b_method'] = x.columns[1]
|
24 |
+
sample['a_feedback'] = x.values[0][0]
|
25 |
+
sample['b_feedback'] = x.values[0][1]
|
26 |
+
return [sample['a_feedback'], sample['b_feedback']]
|
27 |
+
|
28 |
+
# Setup file handling
|
29 |
+
JSON_DATASET_DIR = Path('json_dataset')
|
30 |
+
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
31 |
+
|
32 |
+
JSON_DATASET_PATH = JSON_DATASET_DIR / f"{uuid4()}.json"
|
33 |
+
|
34 |
+
scheduler = CommitScheduler(
|
35 |
+
repo_id='lancewilhelm/cs6804_final',
|
36 |
+
repo_type='dataset',
|
37 |
+
folder_path=JSON_DATASET_DIR,
|
38 |
+
path_in_repo='data'
|
39 |
+
)
|
40 |
+
|
41 |
+
def vote(vote):
|
42 |
+
battle_outcome = {
|
43 |
+
'sample_index': str(sample['index']),
|
44 |
+
'sample_a_method': sample['a_method'],
|
45 |
+
'sample_b_method': sample['b_method']
|
46 |
+
}
|
47 |
+
if vote[0] == 'A':
|
48 |
+
battle_outcome['winner'] = sample['a_method']
|
49 |
+
elif vote[0] == 'B':
|
50 |
+
battle_outcome['winner'] = sample['b_method']
|
51 |
+
elif vote[0] == 'Tie':
|
52 |
+
battle_outcome['winner'] = 'tie'
|
53 |
+
save_json(battle_outcome)
|
54 |
+
|
55 |
+
def save_json(data: dict) -> None:
|
56 |
+
with scheduler.lock:
|
57 |
+
with JSON_DATASET_PATH.open("a") as f:
|
58 |
+
json.dump(data, f)
|
59 |
+
f.write("\n")
|
60 |
+
|
61 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
62 |
+
gr.Markdown("## Feedback Battle")
|
63 |
+
gr.Markdown("Below you will be provided with two pieces of feedback related to a hypothetical leadership communication utterance. Your task is to choose which of the two you feel is more **trustworthy**.")
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
gr.Markdown("### Feedback A")
|
67 |
+
gr.Markdown("### Feedback B")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
feedback_a = gr.Markdown(label='A')
|
71 |
+
feedback_b = gr.Markdown(label='B')
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
gr.Markdown('### Vote Which is More Trustworthy')
|
75 |
+
btn_a = gr.Button("A")
|
76 |
+
btn_b = gr.Button("B")
|
77 |
+
btn_tie = gr.Button("Tie")
|
78 |
+
|
79 |
+
btn_a.click(vote, inputs=gr.State(['A'])).success(get_random_feedback, inputs=None, outputs=[feedback_a, feedback_b])
|
80 |
+
btn_b.click(vote, inputs=gr.State(['B'])).success(get_random_feedback, inputs=None, outputs=[feedback_a, feedback_b])
|
81 |
+
btn_tie.click(vote, inputs=gr.State(['Tie'])).success(get_random_feedback, inputs=None, outputs=[feedback_a, feedback_b])
|
82 |
+
|
83 |
+
app.load(get_random_feedback, inputs=None, outputs=[feedback_a, feedback_b])
|
84 |
+
app.launch()
|