Spaces:
Sleeping
Sleeping
fix reading transcriptions
Browse files
app.py
CHANGED
|
@@ -29,45 +29,55 @@ voice_id = os.getenv("ELEVENLABS_VOICE_ID")
|
|
| 29 |
|
| 30 |
# Load and process the transcripts
|
| 31 |
documents = []
|
| 32 |
-
folder_path = "transcriptions
|
| 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 |
# Check documents length
|
| 70 |
-
print(f"
|
| 71 |
|
| 72 |
# Continue with splitting documents if necessary
|
| 73 |
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
|
|
|
| 29 |
|
| 30 |
# Load and process the transcripts
|
| 31 |
documents = []
|
| 32 |
+
folder_path = "transcriptions" if os.getenv("ENVIRONMENT") == "local" else "transcriptions"
|
| 33 |
+
|
| 34 |
+
def process_transcript_file(file_path):
|
| 35 |
+
with open(file_path, "r", encoding='utf-8') as f:
|
| 36 |
+
content = f.read()
|
| 37 |
+
|
| 38 |
+
# Extract video ID from filename
|
| 39 |
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
| 40 |
+
video_id = base_name.replace("_transcription", "")
|
| 41 |
+
youtube_link_base = f"https://www.youtube.com/watch?v={video_id}"
|
| 42 |
+
|
| 43 |
+
# Parse the transcript content to extract entries with timestamps
|
| 44 |
+
lines = content.splitlines()
|
| 45 |
+
file_documents = []
|
| 46 |
+
for line in lines:
|
| 47 |
+
# Updated regex to match your transcript format
|
| 48 |
+
match = re.match(r"^\[(\d+\.\d+) - (\d+\.\d+)\]\s*(.*)$", line)
|
| 49 |
+
if match:
|
| 50 |
+
start_time = float(match.group(1))
|
| 51 |
+
end_time = float(match.group(2))
|
| 52 |
+
text = match.group(3)
|
| 53 |
+
# Create a Document for each transcript entry
|
| 54 |
+
entry = Document(
|
| 55 |
+
page_content=text,
|
| 56 |
+
metadata={
|
| 57 |
+
"youtube_link": youtube_link_base,
|
| 58 |
+
"start_time": start_time,
|
| 59 |
+
"end_time": end_time,
|
| 60 |
+
"timestamp_link": f"{youtube_link_base}&t={int(start_time)}",
|
| 61 |
+
"channel": os.path.basename(os.path.dirname(file_path)) # Add channel name from folder
|
| 62 |
+
}
|
| 63 |
+
)
|
| 64 |
+
file_documents.append(entry)
|
| 65 |
+
return file_documents
|
| 66 |
+
|
| 67 |
+
# Recursively walk through all subdirectories
|
| 68 |
+
for root, dirs, files in os.walk(folder_path):
|
| 69 |
+
for filename in files:
|
| 70 |
+
if filename.endswith(".txt"):
|
| 71 |
+
file_path = os.path.join(root, filename)
|
| 72 |
+
try:
|
| 73 |
+
file_documents = process_transcript_file(file_path)
|
| 74 |
+
documents.extend(file_documents)
|
| 75 |
+
print(f"Processed {file_path}: {len(file_documents)} entries")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error processing {file_path}: {str(e)}")
|
| 78 |
|
| 79 |
# Check documents length
|
| 80 |
+
print(f"Total number of documents loaded: {len(documents)}")
|
| 81 |
|
| 82 |
# Continue with splitting documents if necessary
|
| 83 |
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|