File size: 2,285 Bytes
cb1a5c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# filename: app.py

import os
import gradio as gr
from dotenv import load_dotenv
from text_analyzer import TextAnalyzer
from log_config import get_logger

os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
load_dotenv()

logger = get_logger('TextAnalysisAppV10')

def launch_gradio():
    """
    Initializes and launches a Gradio web interface for uploading documents and
    performing text analysis. This interface allows users to upload files and
    select types of text analysis to be performed on the uploaded document.
    """
    text_analyzer = TextAnalyzer()
    available_analyses = ["Summary", "Characters", "Plot", "Themes", "Techniques", "Emotional Core", "Narrative Perspective"]

    def process_input(file_path, selected_analyses, progress=gr.Progress()):
        if not file_path:
            return "Please upload a document to proceed."
        if not selected_analyses:
            return "Please select at least one type of analysis."
        if any(analysis not in available_analyses for analysis in selected_analyses):
            return f"Please choose valid options from: {', '.join(available_analyses)}."

        try:
            progress(0.0, desc="Starting analysis...")
            results = text_analyzer.analyze_text(file_path, selected_analyses, progress)
            progress(1.0, desc="Analysis completed.")
            return results or "Analysis completed but no actionable results were found."
        except Exception as e:
            logger.error("Error processing document: %s", str(e), exc_info=True)
            return "Please ensure the file is not corrupted and is in a supported format."

    with gr.Blocks() as demo:
        file_input = gr.File(label="Upload Your Document", type="filepath", file_types=["txt", "pdf", "docx"])
        analysis_options = gr.CheckboxGroup(choices=available_analyses, label="Select Text Analysis Types")
        output_text = gr.Textbox(label="Analysis Results", interactive=True)
        submit_button = gr.Button("Analyze")

        submit_button.click(
            fn=process_input,
            inputs=[file_input, analysis_options],
            outputs=output_text
        )

        demo.launch(server_port=7880, share=True)

if __name__ == "__main__":
    launch_gradio()

# file: app v10.py (end)