Update app.py
Browse files
app.py
CHANGED
|
@@ -7,47 +7,73 @@ from pathlib import Path
|
|
| 7 |
def process_pdf(pdf_file, progress=gr.Progress()):
|
| 8 |
if pdf_file is None:
|
| 9 |
raise gr.Error("Please upload a PDF file")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Clean screenplay
|
| 17 |
-
progress(0.
|
|
|
|
| 18 |
cleaned_path = Path("cleaned_screenplay_long.txt")
|
| 19 |
success = processor.process_screenplay(pdf_file.name, str(cleaned_path))
|
| 20 |
if not success:
|
| 21 |
raise gr.Error("Failed to process screenplay")
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Generate coverage
|
| 24 |
-
progress(0.
|
|
|
|
| 25 |
success = coverage_gen.generate_coverage(cleaned_path)
|
| 26 |
if not success:
|
| 27 |
raise gr.Error("Failed to generate coverage")
|
| 28 |
|
| 29 |
-
# Read results
|
| 30 |
-
with open(cleaned_path, 'r') as f:
|
| 31 |
-
cleaned_text = f.read()
|
| 32 |
with open(Path("coverage.txt"), 'r') as f:
|
| 33 |
coverage = f.read()
|
|
|
|
| 34 |
|
| 35 |
progress(1.0, desc="Complete!")
|
| 36 |
-
return cleaned_text, coverage
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
gr.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
demo.launch()
|
|
|
|
| 7 |
def process_pdf(pdf_file, progress=gr.Progress()):
|
| 8 |
if pdf_file is None:
|
| 9 |
raise gr.Error("Please upload a PDF file")
|
| 10 |
+
|
| 11 |
+
console_output = []
|
| 12 |
+
|
| 13 |
+
def update_console(message):
|
| 14 |
+
console_output.append(message)
|
| 15 |
+
return "\n".join(console_output)
|
| 16 |
|
| 17 |
try:
|
| 18 |
+
# Initialize processors
|
| 19 |
+
progress(0.1, desc="Initializing...")
|
| 20 |
+
processor = GeminiProcessor()
|
| 21 |
+
coverage_gen = CoverageGenerator()
|
| 22 |
+
|
| 23 |
# Clean screenplay
|
| 24 |
+
progress(0.2, desc="Processing screenplay...")
|
| 25 |
+
update_console("Starting screenplay processing...")
|
| 26 |
cleaned_path = Path("cleaned_screenplay_long.txt")
|
| 27 |
success = processor.process_screenplay(pdf_file.name, str(cleaned_path))
|
| 28 |
if not success:
|
| 29 |
raise gr.Error("Failed to process screenplay")
|
| 30 |
+
|
| 31 |
+
with open(cleaned_path, 'r') as f:
|
| 32 |
+
cleaned_text = f.read()
|
| 33 |
+
update_console("Screenplay processing complete!")
|
| 34 |
+
|
| 35 |
# Generate coverage
|
| 36 |
+
progress(0.6, desc="Generating coverage...")
|
| 37 |
+
update_console("Starting coverage generation...")
|
| 38 |
success = coverage_gen.generate_coverage(cleaned_path)
|
| 39 |
if not success:
|
| 40 |
raise gr.Error("Failed to generate coverage")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
with open(Path("coverage.txt"), 'r') as f:
|
| 43 |
coverage = f.read()
|
| 44 |
+
update_console("Coverage generation complete!")
|
| 45 |
|
| 46 |
progress(1.0, desc="Complete!")
|
| 47 |
+
return cleaned_text, coverage, "\n".join(console_output)
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
+
error_msg = f"Error: {str(e)}"
|
| 51 |
+
update_console(error_msg)
|
| 52 |
+
raise gr.Error(error_msg)
|
| 53 |
|
| 54 |
+
with gr.Blocks(title="Screenplay Coverage Generator") as demo:
|
| 55 |
+
gr.Markdown("# Screenplay Coverage Generator")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
file_input = gr.File(label="Upload Screenplay PDF", file_types=[".pdf"])
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
process_btn = gr.Button("Process Screenplay")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
console = gr.Textbox(label="Console Output", lines=5)
|
| 65 |
+
|
| 66 |
+
with gr.Tabs():
|
| 67 |
+
with gr.TabItem("Cleaned Screenplay"):
|
| 68 |
+
cleaned_output = gr.Textbox(label="Cleaned Screenplay", lines=10)
|
| 69 |
+
with gr.TabItem("Coverage"):
|
| 70 |
+
coverage_output = gr.Textbox(label="Coverage Document", lines=10)
|
| 71 |
+
|
| 72 |
+
process_btn.click(
|
| 73 |
+
fn=process_pdf,
|
| 74 |
+
inputs=[file_input],
|
| 75 |
+
outputs=[cleaned_output, coverage_output, console]
|
| 76 |
+
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
demo.launch()
|