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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ORU Transcript Formatter - Hugging Face Spaces Deployment
|
| 3 |
+
AI-Powered Transcript Formatting with ORU Branding
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import tempfile
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from transcript_formatter.core.claude_formatter import format_with_claude
|
| 11 |
+
from docx import Document
|
| 12 |
+
from docx.shared import Inches, Pt
|
| 13 |
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
| 14 |
+
import re
|
| 15 |
+
|
| 16 |
+
def create_word_document(formatted_text, title):
|
| 17 |
+
"""Create a Word document from formatted text."""
|
| 18 |
+
doc = Document()
|
| 19 |
+
|
| 20 |
+
# Set margins
|
| 21 |
+
sections = doc.sections
|
| 22 |
+
for section in sections:
|
| 23 |
+
section.top_margin = Inches(1)
|
| 24 |
+
section.bottom_margin = Inches(1)
|
| 25 |
+
section.left_margin = Inches(1)
|
| 26 |
+
section.right_margin = Inches(1)
|
| 27 |
+
|
| 28 |
+
# Add title
|
| 29 |
+
title_para = doc.add_paragraph()
|
| 30 |
+
title_run = title_para.add_run(title)
|
| 31 |
+
title_run.font.size = Pt(16)
|
| 32 |
+
title_run.bold = True
|
| 33 |
+
title_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 34 |
+
title_para.space_after = Pt(24)
|
| 35 |
+
|
| 36 |
+
# Add metadata
|
| 37 |
+
meta_para = doc.add_paragraph()
|
| 38 |
+
meta_run = meta_para.add_run("Formatted with AI β’ ORU Transcript Formatter")
|
| 39 |
+
meta_run.font.size = Pt(10)
|
| 40 |
+
meta_run.italic = True
|
| 41 |
+
meta_para.space_after = Pt(12)
|
| 42 |
+
|
| 43 |
+
# Add separator
|
| 44 |
+
doc.add_paragraph("_" * 50).space_after = Pt(12)
|
| 45 |
+
|
| 46 |
+
# Process the formatted text and add to document
|
| 47 |
+
lines = formatted_text.split('\n')
|
| 48 |
+
for line in lines:
|
| 49 |
+
line = line.strip()
|
| 50 |
+
if not line:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
para = doc.add_paragraph()
|
| 54 |
+
|
| 55 |
+
# Check if line contains bold formatting (markdown style)
|
| 56 |
+
if '**' in line:
|
| 57 |
+
# Parse markdown-style bold formatting
|
| 58 |
+
parts = re.split(r'(\*\*[^*]+\*\*)', line)
|
| 59 |
+
for part in parts:
|
| 60 |
+
if part.startswith('**') and part.endswith('**'):
|
| 61 |
+
# Bold text
|
| 62 |
+
bold_text = part[2:-2] # Remove ** markers
|
| 63 |
+
run = para.add_run(bold_text)
|
| 64 |
+
run.bold = True
|
| 65 |
+
else:
|
| 66 |
+
# Regular text
|
| 67 |
+
para.add_run(part)
|
| 68 |
+
else:
|
| 69 |
+
# Regular paragraph
|
| 70 |
+
para.add_run(line)
|
| 71 |
+
|
| 72 |
+
# Set font size
|
| 73 |
+
for run in para.runs:
|
| 74 |
+
run.font.size = Pt(11)
|
| 75 |
+
|
| 76 |
+
para.space_after = Pt(6)
|
| 77 |
+
|
| 78 |
+
return doc
|
| 79 |
+
|
| 80 |
+
def format_transcript(file):
|
| 81 |
+
"""Format a transcript file using AI."""
|
| 82 |
+
if file is None:
|
| 83 |
+
return None, "Please upload a transcript file."
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
# Read the uploaded file
|
| 87 |
+
if file.name.endswith('.txt'):
|
| 88 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
| 89 |
+
content = f.read()
|
| 90 |
+
else:
|
| 91 |
+
return None, "Please upload a .txt file."
|
| 92 |
+
|
| 93 |
+
# Format using AI
|
| 94 |
+
formatted_text = format_with_claude(content)
|
| 95 |
+
|
| 96 |
+
# Create Word document
|
| 97 |
+
title = Path(file.name).stem.replace('_', ' ').replace('-', ' ')
|
| 98 |
+
doc = create_word_document(formatted_text, title)
|
| 99 |
+
|
| 100 |
+
# Save to temporary file
|
| 101 |
+
output_path = tempfile.mktemp(suffix='.docx')
|
| 102 |
+
doc.save(output_path)
|
| 103 |
+
|
| 104 |
+
# Return file and success message
|
| 105 |
+
return output_path, "β
Transcript formatted successfully! Download your Word document below."
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return None, f"β Error formatting transcript: {str(e)}"
|
| 109 |
+
|
| 110 |
+
# Custom CSS for ORU branding
|
| 111 |
+
css = """
|
| 112 |
+
.gradio-container {
|
| 113 |
+
background: linear-gradient(135deg, #003366 0%, #002244 100%) !important;
|
| 114 |
+
color: white !important;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
.gr-button-primary {
|
| 118 |
+
background: linear-gradient(135deg, #FFD700 0%, #FFC107 100%) !important;
|
| 119 |
+
color: #003366 !important;
|
| 120 |
+
border: none !important;
|
| 121 |
+
font-weight: bold !important;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
.gr-button-primary:hover {
|
| 125 |
+
background: linear-gradient(135deg, #FFC107 0%, #FFB300 100%) !important;
|
| 126 |
+
transform: translateY(-1px) !important;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
h1 {
|
| 130 |
+
color: #FFD700 !important;
|
| 131 |
+
text-align: center !important;
|
| 132 |
+
font-size: 2.5rem !important;
|
| 133 |
+
margin-bottom: 1rem !important;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
.gr-form {
|
| 137 |
+
background: rgba(255, 255, 255, 0.1) !important;
|
| 138 |
+
border-radius: 15px !important;
|
| 139 |
+
padding: 2rem !important;
|
| 140 |
+
backdrop-filter: blur(10px) !important;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
.gr-file {
|
| 144 |
+
border: 2px dashed #4A90E2 !important;
|
| 145 |
+
border-radius: 10px !important;
|
| 146 |
+
background: rgba(255, 255, 255, 0.05) !important;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
.footer {
|
| 150 |
+
text-align: center !important;
|
| 151 |
+
color: #FFD700 !important;
|
| 152 |
+
margin-top: 2rem !important;
|
| 153 |
+
}
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
# Create Gradio interface
|
| 157 |
+
with gr.Blocks(css=css, title="ORU Transcript Formatter") as demo:
|
| 158 |
+
gr.HTML("""
|
| 159 |
+
<h1>π ORU Transcript Formatter</h1>
|
| 160 |
+
<p style="text-align: center; color: #FFD700; font-size: 1.2rem; margin-bottom: 2rem;">
|
| 161 |
+
AI-Powered Transcript Formatting β’ Oral Roberts University
|
| 162 |
+
</p>
|
| 163 |
+
""")
|
| 164 |
+
|
| 165 |
+
with gr.Row():
|
| 166 |
+
with gr.Column():
|
| 167 |
+
file_input = gr.File(
|
| 168 |
+
label="π Upload Transcript File (.txt)",
|
| 169 |
+
file_types=[".txt"],
|
| 170 |
+
type="filepath"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
format_btn = gr.Button(
|
| 174 |
+
"π€ Format Transcript",
|
| 175 |
+
variant="primary",
|
| 176 |
+
size="lg"
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
status_output = gr.Textbox(
|
| 180 |
+
label="Status",
|
| 181 |
+
interactive=False,
|
| 182 |
+
lines=2
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
with gr.Column():
|
| 186 |
+
file_output = gr.File(
|
| 187 |
+
label="π₯ Download Formatted Document",
|
| 188 |
+
interactive=False
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
gr.HTML("""
|
| 192 |
+
<div class="footer">
|
| 193 |
+
<h3>β¨ Features</h3>
|
| 194 |
+
<p>π― AI-powered speaker detection β’ π Scripture reference highlighting β’ π¨ Professional formatting</p>
|
| 195 |
+
<p>Β© 2025 Oral Roberts University β’ Powered by AI</p>
|
| 196 |
+
</div>
|
| 197 |
+
""")
|
| 198 |
+
|
| 199 |
+
# Connect the interface
|
| 200 |
+
format_btn.click(
|
| 201 |
+
fn=format_transcript,
|
| 202 |
+
inputs=[file_input],
|
| 203 |
+
outputs=[file_output, status_output]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Launch the demo
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
demo.launch(
|
| 209 |
+
server_name="0.0.0.0",
|
| 210 |
+
server_port=7860,
|
| 211 |
+
share=False
|
| 212 |
+
)
|