Threadbourne commited on
Commit
15b63bb
·
verified ·
1 Parent(s): 54ad5b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -40
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(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:
@@ -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
- 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")
@@ -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
+