Move Python code into separate files for better organization

This commit is contained in:
Gigi
2025-04-01 12:10:00 +01:00
parent 597349eecf
commit d06f24e38e
4 changed files with 147 additions and 115 deletions

83
src/extract.py Executable file
View File

@@ -0,0 +1,83 @@
#!/usr/bin/env python3
import sys
import ollama
import re
from pathlib import Path
import time
def determine_content_type(transcript_text: str) -> str:
"""Determine the type of content in the transcript."""
text = transcript_text.lower()
if re.search(r'\bblog post\b', text) or re.search(r'\bdraft\b', text):
return "blog_post"
elif re.search(r'\bidea\b', text) and re.search(r'\bapp\b', text):
return "idea_app"
return "default"
def generate_additional_content(content_type: str, transcript_text: str) -> str:
"""Generate additional content based on the content type."""
prompt_dir = Path("prompts")
with open(prompt_dir / f"{content_type}.md", 'r', encoding='utf-8') as f:
prompt_template = f.read()
prompt = prompt_template.format(transcript=transcript_text)
response = ollama.chat(model='llama2', messages=[
{
'role': 'user',
'content': prompt
}
])
return response['message']['content'].strip()
def main():
if len(sys.argv) != 2:
print("Usage: python extract.py <transcript_file>")
sys.exit(1)
input_file = Path(sys.argv[1])
if not input_file.exists():
print(f"Error: File {input_file} does not exist")
sys.exit(1)
# Set up directory paths
voice_memo_dir = Path("VoiceMemos")
draft_dir = voice_memo_dir / "drafts"
prompt_dir = voice_memo_dir / "prompts"
# Create output directories if they don't exist
draft_dir.mkdir(parents=True, exist_ok=True)
prompt_dir.mkdir(parents=True, exist_ok=True)
print(f"Processing transcript: {input_file}")
print("Extracting content...")
# Read transcript
with open(input_file, 'r', encoding='utf-8') as f:
transcript_text = f.read()
# Determine content type and generate content if needed
content_type = determine_content_type(transcript_text)
if content_type != "default":
print(f" Generating {content_type} content...")
additional_content = generate_additional_content(content_type, transcript_text)
# Save to appropriate directory with timestamp
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = input_file.stem
if content_type == "blog_post":
output_file = draft_dir / f"{filename}_{timestamp}.md"
else: # idea_app
output_file = prompt_dir / f"{filename}_{timestamp}.md"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(additional_content)
print(f" Content saved to: {output_file}")
else:
print(" No blog post or app idea content detected")
print("----------------------------------------")
if __name__ == "__main__":
main()

View File

@@ -15,77 +15,11 @@ if [ ! -f "$input_file" ]; then
exit 1
fi
# Set the directory paths
VOICE_MEMO_DIR="VoiceMemos"
DRAFT_DIR="$VOICE_MEMO_DIR/drafts"
PROMPT_DIR="$VOICE_MEMO_DIR/prompts"
# Create output directories if they don't exist
mkdir -p "$DRAFT_DIR"
mkdir -p "$PROMPT_DIR"
echo "Processing transcript: $input_file"
echo "Extracting content..."
# Activate the virtual environment
source vibenv/bin/activate
# Run the Python script to extract content
python -c "
import ollama
import re
from pathlib import Path
import time
def determine_content_type(transcript_text: str) -> str:
text = transcript_text.lower()
if re.search(r'\bblog post\b', text) or re.search(r'\bdraft\b', text):
return 'blog_post'
elif re.search(r'\bidea\b', text) and re.search(r'\bapp\b', text):
return 'idea_app'
return 'default'
def generate_additional_content(content_type: str, transcript_text: str) -> str:
prompt_dir = Path('prompts')
with open(prompt_dir / f'{content_type}.md', 'r', encoding='utf-8') as f:
prompt_template = f.read()
prompt = prompt_template.format(transcript=transcript_text)
response = ollama.chat(model='llama2', messages=[
{
'role': 'user',
'content': prompt
}
])
return response['message']['content'].strip()
# Read transcript
with open('$input_file', 'r', encoding='utf-8') as f:
transcript_text = f.read()
# Determine content type and generate content if needed
content_type = determine_content_type(transcript_text)
if content_type != 'default':
print(f' Generating {content_type} content...')
additional_content = generate_additional_content(content_type, transcript_text)
# Save to appropriate directory with timestamp
timestamp = time.strftime('%Y%m%d_%H%M%S')
filename = Path('$input_file').stem
if content_type == 'blog_post':
output_file = Path('$DRAFT_DIR') / f'{filename}_{timestamp}.md'
else: # idea_app
output_file = Path('$PROMPT_DIR') / f'{filename}_{timestamp}.md'
with open(output_file, 'w', encoding='utf-8') as f:
f.write(additional_content)
print(f' Content saved to: {output_file}')
else:
print(' No blog post or app idea content detected')
"
# Run the Python script
python extract.py "$input_file"
# Deactivate the virtual environment
deactivate
echo "----------------------------------------"
deactivate

58
src/summarize.py Executable file
View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python3
import sys
import ollama
from pathlib import Path
def generate_summary(transcript_text: str) -> str:
"""Generate a summary of the transcript."""
prompt_dir = Path("prompts")
with open(prompt_dir / "summary.md", 'r', encoding='utf-8') as f:
prompt_template = f.read()
prompt = prompt_template.format(transcript=transcript_text)
response = ollama.chat(model='llama2', messages=[
{
'role': 'user',
'content': prompt
}
])
return response['message']['content'].strip()
def main():
if len(sys.argv) != 2:
print("Usage: python summarize.py <transcript_file>")
sys.exit(1)
input_file = Path(sys.argv[1])
if not input_file.exists():
print(f"Error: File {input_file} does not exist")
sys.exit(1)
# Set up directory paths
voice_memo_dir = Path("VoiceMemos")
summary_dir = voice_memo_dir / "summaries"
summary_dir.mkdir(parents=True, exist_ok=True)
# Get the filename without the path and extension
filename = input_file.stem
summary_file = summary_dir / f"{filename}_summary.txt"
print(f"Processing transcript: {input_file}")
print("Generating summary...")
# Read transcript and generate summary
with open(input_file, 'r', encoding='utf-8') as f:
transcript_text = f.read()
summary = generate_summary(transcript_text)
# Save summary
with open(summary_file, 'w', encoding='utf-8') as f:
f.write(summary)
print(f"Summary saved to: {summary_file}")
print("----------------------------------------")
if __name__ == "__main__":
main()

View File

@@ -15,54 +15,11 @@ if [ ! -f "$input_file" ]; then
exit 1
fi
# Set the directory paths
VOICE_MEMO_DIR="VoiceMemos"
SUMMARY_DIR="$VOICE_MEMO_DIR/summaries"
# Create summaries directory if it doesn't exist
mkdir -p "$SUMMARY_DIR"
# Get the filename without the path and extension
filename=$(basename "$input_file" .txt)
summary_file="$SUMMARY_DIR/${filename}_summary.txt"
echo "Processing transcript: $input_file"
echo "Generating summary..."
# Activate the virtual environment
source vibenv/bin/activate
# Run the Python script to generate the summary
python -c "
import ollama
from pathlib import Path
def generate_summary(transcript_text: str) -> str:
prompt_dir = Path('prompts')
with open(prompt_dir / 'summary.md', 'r', encoding='utf-8') as f:
prompt_template = f.read()
prompt = prompt_template.format(transcript=transcript_text)
response = ollama.chat(model='llama2', messages=[
{
'role': 'user',
'content': prompt
}
])
return response['message']['content'].strip()
# Read transcript
with open('$input_file', 'r', encoding='utf-8') as f:
transcript_text = f.read()
# Generate and save summary
summary = generate_summary(transcript_text)
with open('$summary_file', 'w', encoding='utf-8') as f:
f.write(summary)
"
# Run the Python script
python summarize.py "$input_file"
# Deactivate the virtual environment
deactivate
echo "Summary saved to: $summary_file"
echo "----------------------------------------"
deactivate