Spaces:
Runtime error
Runtime error
Commit
·
d3b2949
1
Parent(s):
eba7dd5
Basic implementation ok
Browse files- README.md +4 -6
- app.py +125 -151
- requirements.txt +2 -1
README.md
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: yellow
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.21.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
python_version: 3.9.13
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Conversation rebuilder
|
| 3 |
+
emoji: 🗣️
|
| 4 |
colorFrom: yellow
|
| 5 |
+
colorTo: orange
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.21.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
python_version: 3.9.13
|
| 11 |
+
---
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,153 +1,127 @@
|
|
| 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 |
-
def diff_texts(text1, text2):
|
| 128 |
-
return text1 + ' ' +text2
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
app = gr.Interface(
|
| 132 |
-
diff_texts,
|
| 133 |
-
[
|
| 134 |
-
gr.Textbox(
|
| 135 |
-
label="Text 1",
|
| 136 |
-
info="Initial text",
|
| 137 |
-
lines=3,
|
| 138 |
-
value="The quick brown fox jumped over the lazy dogs.",
|
| 139 |
-
),
|
| 140 |
-
gr.Textbox(
|
| 141 |
-
label="Text 2",
|
| 142 |
-
info="Text to compare",
|
| 143 |
-
lines=3,
|
| 144 |
-
value="The fast brown fox jumps over lazy dogs.",
|
| 145 |
-
),
|
| 146 |
-
],
|
| 147 |
-
gr.Textbox(
|
| 148 |
-
label="Sum",
|
| 149 |
-
),
|
| 150 |
-
theme=gr.themes.Base()
|
| 151 |
-
)
|
| 152 |
|
| 153 |
app.launch()
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import gradio as gr # type: ignore
|
| 3 |
+
import pandas as pd # type: ignore
|
| 4 |
+
|
| 5 |
+
with gr.Blocks(title="Conversation rebuilder") as app:
|
| 6 |
+
gr.Markdown(
|
| 7 |
+
"""# Conversation rebuilder
|
| 8 |
+
Please, fill the Database Transcript and the List of matched clips from Kibana, and click the Rebuild button"""
|
| 9 |
+
)
|
| 10 |
+
with gr.Row():
|
| 11 |
+
txt_transcript = gr.Code(
|
| 12 |
+
label="Database Transcript",
|
| 13 |
+
interactive=True,
|
| 14 |
+
lines=5,
|
| 15 |
+
)
|
| 16 |
+
txt_clips = gr.Code(
|
| 17 |
+
label="Kibana clips",
|
| 18 |
+
interactive=True,
|
| 19 |
+
lines=5,
|
| 20 |
+
)
|
| 21 |
+
with gr.Row():
|
| 22 |
+
gr.ClearButton(value="Clear", variant="secondary", size="sm", components=[txt_transcript, txt_clips])
|
| 23 |
+
btn_build = gr.Button(value="Rebuild", variant="primary", size="sm")
|
| 24 |
+
with gr.Row():
|
| 25 |
+
data = gr.Dataframe(
|
| 26 |
+
label="CONVERSATION",
|
| 27 |
+
headers=["index", "user", "agent", "gpt", "distance"],
|
| 28 |
+
datatype=["str", "str", "str", "str", "number"],
|
| 29 |
+
column_widths=["8%","29%","29%","29%","5%"],
|
| 30 |
+
# row_count=(1, "fixed"),
|
| 31 |
+
col_count=(5, "fixed"),
|
| 32 |
+
interactive=False,
|
| 33 |
+
wrap=True,
|
| 34 |
+
)
|
| 35 |
+
with gr.Row():
|
| 36 |
+
file = gr.File(
|
| 37 |
+
label="Export files",
|
| 38 |
+
show_label=True,
|
| 39 |
+
height=60,
|
| 40 |
+
container=True,
|
| 41 |
+
interactive=False,
|
| 42 |
+
file_count="single",
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
COL_TIMESTAMP = 0
|
| 46 |
+
COL_CONVERSATION_ID = 1
|
| 47 |
+
COL_CLIP_COLLECTION_ID = 2
|
| 48 |
+
COL_REQUEST_ID = 2
|
| 49 |
+
COL_SENTENCE_INDEX = 4
|
| 50 |
+
COL_SENTENCE_ORIGINAL = 5
|
| 51 |
+
COL_CLIP_TEXT = 6
|
| 52 |
+
COL_CLIP_ID = 7
|
| 53 |
+
COL_DISTANCE = 8
|
| 54 |
+
|
| 55 |
+
def find_clips_matched(agent_text,clips):
|
| 56 |
+
clip_list = clips.splitlines()
|
| 57 |
+
for clip in clip_list:
|
| 58 |
+
parts = clip.strip().split('\t')
|
| 59 |
+
if parts[COL_CLIP_TEXT] == agent_text:
|
| 60 |
+
return \
|
| 61 |
+
parts[COL_SENTENCE_ORIGINAL], \
|
| 62 |
+
int(parts[COL_SENTENCE_INDEX]), \
|
| 63 |
+
round(float(parts[COL_DISTANCE]),2)
|
| 64 |
+
|
| 65 |
+
@btn_build.click(inputs=[txt_transcript,txt_clips], outputs=[data, file])
|
| 66 |
+
def rebuild_conversation(transcript, clips):
|
| 67 |
+
df = pd.DataFrame({"index": [], "user": [], "agent": [], "gpt": [], "distance": []})
|
| 68 |
+
if not transcript.strip() or not clips.strip():
|
| 69 |
+
msg = f"EMPTY TRANSCRIPT OR LIST OF CLIPS!"
|
| 70 |
+
df.loc[len(df.index)] = ["", msg, "", "", ""]
|
| 71 |
+
return df, None
|
| 72 |
+
lines = transcript.splitlines()
|
| 73 |
+
user_text = ""
|
| 74 |
+
conversation_line = 1
|
| 75 |
+
for i in range(len(lines)):
|
| 76 |
+
line = lines[i].strip()
|
| 77 |
+
if line:
|
| 78 |
+
if line.startswith("user:"):
|
| 79 |
+
user_text = line.replace("user:","").strip()
|
| 80 |
+
conversation_line = conversation_line + 1
|
| 81 |
+
elif line.startswith("agent:"):
|
| 82 |
+
agent_text = line.replace("agent:","").strip()
|
| 83 |
+
gpt, order, distance = find_clips_matched(agent_text,clips)
|
| 84 |
+
index = f"{str(conversation_line).zfill(3)}-{str(order).zfill(2)}"
|
| 85 |
+
df.loc[len(df.index)] = [index, user_text, agent_text, gpt, distance]
|
| 86 |
+
user_text = ""
|
| 87 |
+
# Get conversation ID
|
| 88 |
+
clip_list = clips.splitlines()
|
| 89 |
+
parts = clip_list[0].strip().split('\t')
|
| 90 |
+
conversation_id = parts[COL_CONVERSATION_ID]
|
| 91 |
+
tsv_file_name = f'conversation-{conversation_id}.tsv'
|
| 92 |
+
excel_file_name = f'conversation-{conversation_id}.xlsx'
|
| 93 |
+
# Build TSV file
|
| 94 |
+
df.to_csv(
|
| 95 |
+
tsv_file_name,
|
| 96 |
+
sep="\t",
|
| 97 |
+
encoding="utf-8",
|
| 98 |
+
index=False,
|
| 99 |
+
header=True,
|
| 100 |
+
quoting=csv.QUOTE_ALL,
|
| 101 |
+
)
|
| 102 |
+
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html
|
| 103 |
+
df_excel = df.copy(deep=True)
|
| 104 |
+
# https://blog.enterprisedna.co/pandas-drop-index/
|
| 105 |
+
df_excel.set_index('index', inplace=True)
|
| 106 |
+
# Build Excel file
|
| 107 |
+
# https://xlsxwriter.readthedocs.io/working_with_pandas.html
|
| 108 |
+
# Create a Pandas Excel writer using XlsxWriter as the engine.
|
| 109 |
+
writer = pd.ExcelWriter(excel_file_name, engine='xlsxwriter')
|
| 110 |
+
# Convert the dataframe to an XlsxWriter Excel object.
|
| 111 |
+
df_excel.to_excel(writer, sheet_name='Conversation')
|
| 112 |
+
# Get the xlsxwriter workbook and worksheet objects.
|
| 113 |
+
workbook = writer.book
|
| 114 |
+
worksheet = writer.sheets["Conversation"]
|
| 115 |
+
# https://xlsxwriter.readthedocs.io/format.html#number-formats-in-different-locales
|
| 116 |
+
number_format = workbook.add_format({'num_format': '#,##0.00'})
|
| 117 |
+
text_format = workbook.add_format({'text_wrap': True})
|
| 118 |
+
# Set the columns widths.
|
| 119 |
+
worksheet.set_column("B:D", 50, text_format)
|
| 120 |
+
worksheet.set_column('E:E', 8, number_format)
|
| 121 |
+
# Autofit the worksheet.
|
| 122 |
+
worksheet.autofit()
|
| 123 |
+
# Close the Pandas Excel writer and output the Excel file.
|
| 124 |
+
writer.close()
|
| 125 |
+
return df, [excel_file_name,tsv_file_name]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
app.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
gradio
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
xlsxwriter
|