Threadbourne commited on
Commit
f02d09b
·
verified ·
1 Parent(s): 193ab4f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import pandas as pd
3
+ 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
21
+ )
22
+
23
+ parts = pattern.split(text)
24
+
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()
32
+
33
+ if not content:
34
+ continue
35
+
36
+ speaker = "user" if marker == user_marker.lower() else "gpt"
37
+ turn += 1
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({
46
+ "turn": turn,
47
+ "speaker": speaker,
48
+ "chars": chars,
49
+ "words": words,
50
+ "tokens_est": tokens_est
51
+ })
52
+
53
+ return pd.DataFrame(rows)
54
+
55
+
56
+ def convert(file, user_marker, assistant_marker, preview_rows):
57
+ if file is None:
58
+ return None, None, "Upload a .md or .txt transcript."
59
+
60
+ with open(file.name, "r", encoding="utf-8", errors="replace") as f:
61
+ text = f.read()
62
+
63
+ # Basic sanity check: markers present?
64
+ if user_marker.lower() not in text.lower() and assistant_marker.lower() not in text.lower():
65
+ return None, None, (
66
+ "No markers found in the file.\n"
67
+ "Update the marker strings to match your transcript format "
68
+ f"(e.g., '{user_marker}' / '{assistant_marker}')."
69
+ )
70
+
71
+ df = parse_transcript(text, user_marker=user_marker, assistant_marker=assistant_marker)
72
+
73
+ if df.empty:
74
+ return None, None, (
75
+ "Parsed 0 turns. Markers may not match the transcript format, "
76
+ "or content blocks may be empty."
77
+ )
78
+
79
+ pr = int(preview_rows)
80
+ pr = max(5, min(pr, 100))
81
+ preview = df.head(pr)
82
+
83
+ out_path = "/mnt/data/converted_turns.csv"
84
+ df.to_csv(out_path, index=False)
85
+
86
+ msg = f"Parsed {len(df)} turns. Download the CSV below."
87
+ return preview, out_path, msg
88
+
89
+
90
+ with gr.Blocks(title="Markdown → CSV Converter") as demo:
91
+ gr.Markdown(
92
+ "### Markdown → CSV Converter\n"
93
+ "Upload a transcript (.md/.txt) and convert it into a canonical turn-level CSV.\n\n"
94
+ "**Privacy:** Files are processed in-session and not stored."
95
+ )
96
+
97
+ file = gr.File(label="Upload transcript", file_types=[".md", ".txt"])
98
+
99
+ with gr.Row():
100
+ user_marker = gr.Textbox(value="You said:", label="User marker")
101
+ assistant_marker = gr.Textbox(value="ChatGPT said:", label="Assistant marker")
102
+
103
+ preview_rows = gr.Slider(5, 100, value=20, step=1, label="Preview rows")
104
+
105
+ btn = gr.Button("Convert")
106
+
107
+ preview = gr.Dataframe(label="Preview", interactive=False, wrap=True)
108
+ out_file = gr.File(label="Download CSV")
109
+ status = gr.Textbox(label="Status", interactive=False)
110
+
111
+ btn.click(
112
+ fn=convert,
113
+ inputs=[file, user_marker, assistant_marker, preview_rows],
114
+ outputs=[preview, out_file, status],
115
+ )
116
+
117
+ demo.launch()