File size: 5,607 Bytes
71773fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import gradio as gr
from agent_state import AgentState
from workflow import build_graph
from tools.transcriber import transcribe_fast # We need to import the tool directly
from datetime import datetime

# --- Main Application Logic ---

# Build the graph once when the app starts
app = build_graph()

# This function is now ONLY for transcription
def run_transcription(audio_file):
    """

    Step 1: Runs ONLY the transcription tool and prepares the state.

    """
    if not audio_file:
        return None, None, gr.update(visible=False)

    print("--- Step 1: Transcribing Audio ---")
    # Create a new agent state for this session
    initial_state = AgentState(audio_path=audio_file)
    
    # Call the transcription tool directly
    state_after_transcription = transcribe_fast(initial_state)

    # Return the text for the UI, the updated state for the session,
    # and make the next button visible.
    return (
        state_after_transcription,
        state_after_transcription.transcribed_text,
        gr.update(visible=True)
    )

# This new function handles the rest of the agent's workflow
def generate_report(current_state):
    """

    Step 2: Takes the transcribed state and runs it through the main graph

    to generate the report and enable the review process.

    """
    if not current_state or not current_state.transcribed_text:
        return current_state, "Transcription not found. Please complete Step 1.", gr.update(visible=False)

    print("--- Step 2: Generating News Report ---")
    
    final_state = None
    # Run the stream. The graph will start from the news_reporter node
    # because the transcribed_text already exists.
    # Note: Your graph needs to be robust enough to handle this.
    # A simple way is to have the first node check if transcription exists.
    # If not, run it. If yes, skip to the next node.
    # For now, we assume the graph continues from where the state is.
    
    # A simplified invocation for this flow would be to call the reporter tool directly
    # and then handle the loop, but let's stick to the graph.
    # To make this work, we'll manually call the next step for clarity.
    
    from tools.news_reporter import create_news_report # this is our tool
    state_after_report = create_news_report(current_state)

    return state_after_report, state_after_report.news_report, gr.update(visible=True)


def handle_revision(feedback, current_state):
    """

    Handles the revision loop.

    """
    if not feedback:
        return current_state, current_state.news_report, "Please provide feedback to revise."

    print("Revising with feedback...")
    current_state.feedback = feedback
    current_state.approved = False
    
    # Re-run the summarization/report tool with the new feedback
    from tools.news_reporter import create_news_report
    state_after_revision = create_news_report(current_state)

    return state_after_revision, state_after_revision.news_report, "βœ… Report revised. Please review again."


def handle_save(current_state):
    """

    Handles the final save action.

    """
    print("Saving final report...")
    from tools.saver import save_summary
    save_summary(current_state) # Call the save tool directly
    
    return "βœ… Final report has been saved successfully!"




#####################################
# --- Gradio UI ---
#####################################
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸŽ™οΈ Audio to News Agent")
    gr.Markdown(f"### Reporting live, {datetime.now().strftime('%B %d, %Y')}.")
    
    agent_state_gr = gr.State(value=None)

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### Step 1: Transcribe Audio")
            audio_input = gr.Audio(type="filepath", sources=["upload", "microphone"], label="Upload or Record")
            transcribe_btn = gr.Button("1️⃣ Transcribe Audio", variant="secondary")
            
            # This button will appear after step 1 is complete
            generate_btn = gr.Button("2️⃣ Generate News Report", variant="primary", visible=False)
            
        with gr.Column(scale=2):
            transcribed_output = gr.Textbox(label="πŸ“ Transcription Result", lines=5, interactive=False)
            report_output = gr.Textbox(label="πŸ“° Generated News Report", lines=10, interactive=False)
            
            with gr.Group(visible=False) as review_group:
                feedback_input = gr.Textbox(label="❌ Provide Feedback for Revision", lines=2)
                with gr.Row():
                    revise_btn = gr.Button("πŸ” Revise Report")
                    save_btn = gr.Button("βœ… Approve & Save Report", variant="primary")
                status_output = gr.Textbox(label="Status", interactive=False)

    # --- Button Click Logic ---

    transcribe_btn.click(
        fn=run_transcription,
        inputs=[audio_input],
        outputs=[agent_state_gr, transcribed_output, generate_btn]
    )
    
    generate_btn.click(
        fn=generate_report,
        inputs=[agent_state_gr],
        outputs=[agent_state_gr, report_output, review_group]
    )

    revise_btn.click(
        fn=handle_revision,
        inputs=[feedback_input, agent_state_gr],
        outputs=[agent_state_gr, report_output, status_output]
    )

    save_btn.click(
        fn=handle_save,
        inputs=[agent_state_gr],
        outputs=[status_output]
    )

demo.launch(share=True)