Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,6 @@ import gradio as gr
|
|
| 4 |
|
| 5 |
|
| 6 |
def parse_transcript(text: str, user_marker: str, assistant_marker: str) -> pd.DataFrame:
|
| 7 |
-
"""
|
| 8 |
-
Parse a transcript with explicit markers into canonical turn-level metrics.
|
| 9 |
-
|
| 10 |
-
Expected style:
|
| 11 |
-
You said: <text...>
|
| 12 |
-
ChatGPT said: <text...>
|
| 13 |
-
|
| 14 |
-
Markers are configurable.
|
| 15 |
-
"""
|
| 16 |
-
# Build a regex that splits on either marker
|
| 17 |
-
# We capture the marker so we know which speaker it was.
|
| 18 |
pattern = re.compile(
|
| 19 |
rf"({re.escape(user_marker)}|{re.escape(assistant_marker)})\s*",
|
| 20 |
re.IGNORECASE
|
|
@@ -25,7 +14,6 @@ def parse_transcript(text: str, user_marker: str, assistant_marker: str) -> pd.D
|
|
| 25 |
rows = []
|
| 26 |
turn = 0
|
| 27 |
|
| 28 |
-
# parts looks like: [preamble, marker, content, marker, content, ...]
|
| 29 |
for i in range(1, len(parts), 2):
|
| 30 |
marker = parts[i].strip().lower()
|
| 31 |
content = parts[i + 1].strip()
|
|
@@ -38,8 +26,6 @@ def parse_transcript(text: str, user_marker: str, assistant_marker: str) -> pd.D
|
|
| 38 |
|
| 39 |
chars = len(content)
|
| 40 |
words = len(content.split())
|
| 41 |
-
|
| 42 |
-
# Lightweight token estimate. (You can adjust later.)
|
| 43 |
tokens_est = int(round(words * 1.33))
|
| 44 |
|
| 45 |
rows.append({
|
|
@@ -53,38 +39,41 @@ def parse_transcript(text: str, user_marker: str, assistant_marker: str) -> pd.D
|
|
| 53 |
return pd.DataFrame(rows)
|
| 54 |
|
| 55 |
|
| 56 |
-
def convert(
|
| 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 |
with gr.Blocks(title="Markdown → CSV Converter") as demo:
|
|
@@ -94,7 +83,8 @@ with gr.Blocks(title="Markdown → CSV Converter") as demo:
|
|
| 94 |
"**Privacy:** Files are processed in-session and not stored."
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
user_marker = gr.Textbox(value="You said:", label="User marker")
|
|
@@ -115,3 +105,4 @@ with gr.Blocks(title="Markdown → CSV Converter") as demo:
|
|
| 115 |
)
|
| 116 |
|
| 117 |
demo.launch()
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def parse_transcript(text: str, user_marker: str, assistant_marker: str) -> pd.DataFrame:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
pattern = re.compile(
|
| 8 |
rf"({re.escape(user_marker)}|{re.escape(assistant_marker)})\s*",
|
| 9 |
re.IGNORECASE
|
|
|
|
| 14 |
rows = []
|
| 15 |
turn = 0
|
| 16 |
|
|
|
|
| 17 |
for i in range(1, len(parts), 2):
|
| 18 |
marker = parts[i].strip().lower()
|
| 19 |
content = parts[i + 1].strip()
|
|
|
|
| 26 |
|
| 27 |
chars = len(content)
|
| 28 |
words = len(content.split())
|
|
|
|
|
|
|
| 29 |
tokens_est = int(round(words * 1.33))
|
| 30 |
|
| 31 |
rows.append({
|
|
|
|
| 39 |
return pd.DataFrame(rows)
|
| 40 |
|
| 41 |
|
| 42 |
+
def convert(file_path, user_marker, assistant_marker, preview_rows):
|
| 43 |
+
try:
|
| 44 |
+
if not file_path:
|
| 45 |
+
return None, None, "Upload a .md or .txt transcript."
|
| 46 |
|
| 47 |
+
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
|
| 48 |
+
text = f.read()
|
| 49 |
|
| 50 |
+
if user_marker.lower() not in text.lower() and assistant_marker.lower() not in text.lower():
|
| 51 |
+
return None, None, (
|
| 52 |
+
"No markers found in the file.\n"
|
| 53 |
+
"Update the marker strings to match your transcript format "
|
| 54 |
+
f"(e.g., '{user_marker}' / '{assistant_marker}')."
|
| 55 |
+
)
|
|
|
|
| 56 |
|
| 57 |
+
df = parse_transcript(text, user_marker=user_marker, assistant_marker=assistant_marker)
|
| 58 |
|
| 59 |
+
if df.empty:
|
| 60 |
+
return None, None, (
|
| 61 |
+
"Parsed 0 turns. Markers may not match the transcript format, "
|
| 62 |
+
"or content blocks may be empty."
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
pr = int(preview_rows)
|
| 66 |
+
pr = max(5, min(pr, 100))
|
| 67 |
+
preview = df.head(pr)
|
| 68 |
|
| 69 |
+
out_path = "/mnt/data/converted_turns.csv"
|
| 70 |
+
df.to_csv(out_path, index=False)
|
| 71 |
|
| 72 |
+
msg = f"Parsed {len(df)} turns. Download the CSV below."
|
| 73 |
+
return preview, out_path, msg
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return None, None, f"Conversion failed: {type(e).__name__}: {e}"
|
| 77 |
|
| 78 |
|
| 79 |
with gr.Blocks(title="Markdown → CSV Converter") as demo:
|
|
|
|
| 83 |
"**Privacy:** Files are processed in-session and not stored."
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# IMPORTANT: type="filepath" makes this stable on HF/Gradio
|
| 87 |
+
file = gr.File(label="Upload transcript", file_types=[".md", ".txt"], type="filepath")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
user_marker = gr.Textbox(value="You said:", label="User marker")
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
demo.launch()
|
| 108 |
+
|