Spaces:
Running
Running
init
Browse files- app.py +141 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pandas as pd
|
3 |
+
import warnings
|
4 |
+
from ast import literal_eval
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
warnings.filterwarnings('ignore')
|
8 |
+
|
9 |
+
# Load the CSV file.
|
10 |
+
# For Hugging Face Spaces, place the CSV file in your repository (or update the path accordingly).
|
11 |
+
df = pd.read_csv('df_chatbot_response.csv')
|
12 |
+
|
13 |
+
# Create new conversation columns for variants A and B.
|
14 |
+
df['full_conversation_a'] = ''
|
15 |
+
df['full_conversation_b'] = ''
|
16 |
+
|
17 |
+
for i in range(len(df)):
|
18 |
+
a = literal_eval(df['history_conversation'][i])
|
19 |
+
a.append({'AI': df['existing_answer'][i]})
|
20 |
+
b = literal_eval(df['history_conversation'][i])
|
21 |
+
b.append({'AI': df['new_answer'][i]})
|
22 |
+
df.at[i, 'full_conversation_a'] = a
|
23 |
+
df.at[i, 'full_conversation_b'] = b
|
24 |
+
|
25 |
+
# Store conversation rounds as a list of dictionaries.
|
26 |
+
conversation_rounds = []
|
27 |
+
for i in range(len(df)):
|
28 |
+
a = df['full_conversation_a'][i]
|
29 |
+
# Replace literal "\n" with actual newline characters.
|
30 |
+
for d in a:
|
31 |
+
for key, value in d.items():
|
32 |
+
if isinstance(value, str):
|
33 |
+
d[key] = value.replace('\\n', '\n')
|
34 |
+
b = df['full_conversation_b'][i]
|
35 |
+
for d in b:
|
36 |
+
for key, value in d.items():
|
37 |
+
if isinstance(value, str):
|
38 |
+
d[key] = value.replace('\\n', '\n')
|
39 |
+
data_conv = {"a": df['full_conversation_a'][i], "b": df['full_conversation_b'][i]}
|
40 |
+
conversation_rounds.append(data_conv)
|
41 |
+
|
42 |
+
# --- Helper Function to Format Conversations ---
|
43 |
+
def format_conversation(conv):
|
44 |
+
"""
|
45 |
+
Convert a list of dictionaries (with 'human' and 'AI' keys) into a list of tuples,
|
46 |
+
pairing each human message with its subsequent AI response.
|
47 |
+
"""
|
48 |
+
pairs = []
|
49 |
+
i = 0
|
50 |
+
while i < len(conv):
|
51 |
+
if 'human' in conv[i]:
|
52 |
+
human_msg = conv[i]['human']
|
53 |
+
# If the next message exists and is from AI, pair them
|
54 |
+
if i + 1 < len(conv) and 'AI' in conv[i+1]:
|
55 |
+
ai_msg = conv[i+1]['AI']
|
56 |
+
pairs.append((human_msg, ai_msg))
|
57 |
+
i += 2
|
58 |
+
else:
|
59 |
+
pairs.append((human_msg, ""))
|
60 |
+
i += 1
|
61 |
+
else:
|
62 |
+
# If conversation starts with an AI message
|
63 |
+
if 'AI' in conv[i]:
|
64 |
+
pairs.append(("", conv[i]['AI']))
|
65 |
+
i += 1
|
66 |
+
return pairs
|
67 |
+
|
68 |
+
def get_conversation(round_idx):
|
69 |
+
"""Return formatted conversation pairs for a given round index."""
|
70 |
+
if round_idx < len(conversation_rounds):
|
71 |
+
conv = conversation_rounds[round_idx]
|
72 |
+
existing_pairs = format_conversation(conv["a"])
|
73 |
+
new_pairs = format_conversation(conv["b"])
|
74 |
+
return existing_pairs, new_pairs
|
75 |
+
else:
|
76 |
+
# End-of-conversation message if no more rounds.
|
77 |
+
return [("End of conversation.", "")], [("End of conversation.", "")]
|
78 |
+
|
79 |
+
def update_conversation(choice, current_idx, choices_list):
|
80 |
+
"""
|
81 |
+
Update the conversation round when the user clicks the button.
|
82 |
+
- 'choice' is the user's selection ("A" or "B") for the current round.
|
83 |
+
- 'current_idx' is the current conversation round index.
|
84 |
+
- 'choices_list' accumulates all the user's choices.
|
85 |
+
"""
|
86 |
+
choices_list = choices_list + [choice]
|
87 |
+
new_idx = current_idx + 1
|
88 |
+
|
89 |
+
if new_idx >= len(conversation_rounds):
|
90 |
+
summary_text = "Semua percakapan selesai.\nPilihan pengguna per ronde: " + ", ".join(choices_list)
|
91 |
+
# Display the summary in both chatbots when done.
|
92 |
+
return new_idx, [("End of conversation.", summary_text)], [("End of conversation.", summary_text)], choices_list
|
93 |
+
else:
|
94 |
+
existing_pairs, new_pairs = get_conversation(new_idx)
|
95 |
+
return new_idx, existing_pairs, new_pairs, choices_list
|
96 |
+
|
97 |
+
# --- Gradio UI Setup ---
|
98 |
+
with gr.Blocks() as demo:
|
99 |
+
gr.Markdown("## Perbandingan Percakapan")
|
100 |
+
|
101 |
+
# States to track the current round and store user choices.
|
102 |
+
conversation_index = gr.State(0)
|
103 |
+
user_choices = gr.State([])
|
104 |
+
|
105 |
+
# Display the two conversation variants side by side.
|
106 |
+
with gr.Row():
|
107 |
+
existing_chat = gr.Chatbot(label="A")
|
108 |
+
new_chat = gr.Chatbot(label="B")
|
109 |
+
|
110 |
+
# Initialize the chatbots with the first conversation round.
|
111 |
+
initial_existing, initial_new = get_conversation(0)
|
112 |
+
existing_chat.value = initial_existing
|
113 |
+
new_chat.value = initial_new
|
114 |
+
|
115 |
+
# Radio button for the user to choose their preferred conversation variant.
|
116 |
+
flag_choice = gr.Radio(choices=["A", "B"], label="Pilih percakapan yang lebih disukai:")
|
117 |
+
next_button = gr.Button("Percakapan Berikutnya")
|
118 |
+
|
119 |
+
# Textbox to display the final summary of user choices.
|
120 |
+
summary_output = gr.Textbox(label="Ringkasan Pilihan", interactive=False)
|
121 |
+
|
122 |
+
next_button.click(
|
123 |
+
update_conversation,
|
124 |
+
inputs=[flag_choice, conversation_index, user_choices],
|
125 |
+
outputs=[conversation_index, existing_chat, new_chat, user_choices]
|
126 |
+
)
|
127 |
+
|
128 |
+
def show_summary(choices):
|
129 |
+
choices_a = choices.count("A")
|
130 |
+
choices_b = choices.count("B")
|
131 |
+
return f"A: {choices_a}\nB: {choices_b}"
|
132 |
+
|
133 |
+
gr.Button("Tampilkan Ringkasan").click(
|
134 |
+
show_summary,
|
135 |
+
inputs=[user_choices],
|
136 |
+
outputs=[summary_output]
|
137 |
+
)
|
138 |
+
|
139 |
+
# Launch the Gradio app.
|
140 |
+
if __name__ == "__main__":
|
141 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
pandas
|
3 |
+
gradio
|