Alexandros Popov
		
	commited on
		
		
					Commit 
							
							·
						
						5fa4073
	
1
								Parent(s):
							
							c049350
								
added logs to gradio app.
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -1,3 +1,5 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 1 | 
         
             
            import os
         
     | 
| 2 | 
         
             
            import tempfile
         
     | 
| 3 | 
         | 
| 
         @@ -7,50 +9,61 @@ from PIL import Image 
     | 
|
| 7 | 
         
             
            from agents import run_photo_enchancement_agent
         
     | 
| 8 | 
         | 
| 9 | 
         | 
| 10 | 
         
            -
            def process_image_with_agents(image, prompt):
         
     | 
| 11 | 
         
            -
                 
     | 
| 
         | 
|
| 12 | 
         
             
                temp_dir = tempfile.mkdtemp(prefix="gradio_aiart_")
         
     | 
| 13 | 
         
             
                input_path = os.path.join(temp_dir, "input.jpg")
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 14 | 
         
             
                image.save(input_path)
         
     | 
| 15 | 
         | 
| 16 | 
         
            -
                 
     | 
| 
         | 
|
| 17 | 
         | 
| 18 | 
         
            -
                #  
     | 
| 19 | 
         
            -
                 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 20 | 
         | 
| 21 | 
         
            -
                #  
     | 
| 22 | 
         
            -
                 
     | 
| 23 | 
         | 
| 24 | 
         
            -
                #  
     | 
| 25 | 
         
             
                final_image = Image.open(output_path)
         
     | 
| 
         | 
|
| 26 | 
         | 
| 27 | 
         
            -
                # Stream final result
         
     | 
| 28 | 
         
            -
                yield [[input_path, output_path], ["Original", "Final (after agent workflow)"], final_image]
         
     | 
| 29 | 
         | 
| 30 | 
         
            -
             
     | 
| 31 | 
         
            -
            with gr.Blocks(title="AI Art Director : Agent Workflow") as demo:
         
     | 
| 32 | 
         
             
                gr.Markdown(
         
     | 
| 33 | 
         
            -
                    "# 
     | 
| 34 | 
         
            -
                    "Upload an image and describe the vibe you want 
     | 
| 35 | 
         
            -
                    " 
     | 
| 
         | 
|
| 36 | 
         
             
                )
         
     | 
| 
         | 
|
| 37 | 
         
             
                with gr.Row():
         
     | 
| 38 | 
         
             
                    with gr.Column():
         
     | 
| 39 | 
         
             
                        image_input = gr.Image(type="pil", label="Upload Image")
         
     | 
| 40 | 
         
            -
                        prompt_input = gr.Textbox(
         
     | 
| 41 | 
         
            -
                            label="Describe the vibe you want", placeholder="e.g. dreamy, vintage, vibrant..."
         
     | 
| 42 | 
         
            -
                        )
         
     | 
| 43 | 
         
             
                        submit_btn = gr.Button("Go!")
         
     | 
| 44 | 
         
             
                    with gr.Column():
         
     | 
| 45 | 
         
            -
                         
     | 
| 46 | 
         
            -
                         
     | 
| 47 | 
         
            -
                        final = gr.Image(label="Final Image", show_label=True)
         
     | 
| 48 | 
         | 
| 49 | 
         
             
                submit_btn.click(
         
     | 
| 50 | 
         
             
                    process_image_with_agents,
         
     | 
| 51 | 
         
             
                    inputs=[image_input, prompt_input],
         
     | 
| 52 | 
         
            -
                    outputs=[ 
     | 
| 53 | 
         
             
                )
         
     | 
| 54 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 55 | 
         
             
            if __name__ == "__main__":
         
     | 
| 56 | 
         
             
                demo.launch()
         
     | 
| 
         | 
|
| 1 | 
         
            +
            import contextlib
         
     | 
| 2 | 
         
            +
            import io
         
     | 
| 3 | 
         
             
            import os
         
     | 
| 4 | 
         
             
            import tempfile
         
     | 
| 5 | 
         | 
| 
         | 
|
| 9 | 
         
             
            from agents import run_photo_enchancement_agent
         
     | 
| 10 | 
         | 
| 11 | 
         | 
| 12 | 
         
            +
            def process_image_with_agents(image: Image.Image, prompt: str):
         
     | 
| 13 | 
         
            +
                """Stream intermediate steps **and** the agent's stdout / stderr logs."""
         
     | 
| 14 | 
         
            +
                # 🔧 1. Create temp dir & paths
         
     | 
| 15 | 
         
             
                temp_dir = tempfile.mkdtemp(prefix="gradio_aiart_")
         
     | 
| 16 | 
         
             
                input_path = os.path.join(temp_dir, "input.jpg")
         
     | 
| 17 | 
         
            +
                output_path = os.path.join(temp_dir, "output.jpg")
         
     | 
| 18 | 
         
            +
             
     | 
| 19 | 
         
            +
                # 💾 2. Persist original upload
         
     | 
| 20 | 
         
             
                image.save(input_path)
         
     | 
| 21 | 
         | 
| 22 | 
         
            +
                # 🖼️ 3. Yield the original image immediately
         
     | 
| 23 | 
         
            +
                yield image, "Original image uploaded. Starting enhancement…"
         
     | 
| 24 | 
         | 
| 25 | 
         
            +
                # 📝 4. Capture logs while the agent runs
         
     | 
| 26 | 
         
            +
                log_buffer = io.StringIO()
         
     | 
| 27 | 
         
            +
                with contextlib.redirect_stdout(log_buffer), contextlib.redirect_stderr(log_buffer):
         
     | 
| 28 | 
         
            +
                    _ = run_photo_enchancement_agent(
         
     | 
| 29 | 
         
            +
                        prompt,
         
     | 
| 30 | 
         
            +
                        image_path=input_path,
         
     | 
| 31 | 
         
            +
                        output_path=output_path,
         
     | 
| 32 | 
         
            +
                    )
         
     | 
| 33 | 
         | 
| 34 | 
         
            +
                # 🧾 All logs produced by the agent
         
     | 
| 35 | 
         
            +
                logs = log_buffer.getvalue()
         
     | 
| 36 | 
         | 
| 37 | 
         
            +
                # 🖼️ 5. Yield the final image plus the complete logs
         
     | 
| 38 | 
         
             
                final_image = Image.open(output_path)
         
     | 
| 39 | 
         
            +
                yield final_image, f"✅ Enhancement finished.\n\n--- Agent Logs ---\n{logs}"
         
     | 
| 40 | 
         | 
| 
         | 
|
| 
         | 
|
| 41 | 
         | 
| 42 | 
         
            +
            with gr.Blocks(title="AI Art Director • Agent Workflow") as demo:
         
     | 
| 
         | 
|
| 43 | 
         
             
                gr.Markdown(
         
     | 
| 44 | 
         
            +
                    "# AI Art Director\n"
         
     | 
| 45 | 
         
            +
                    "Upload an image and describe the vibe you want.\n"
         
     | 
| 46 | 
         
            +
                    "The agent will propose, apply, and critique edits to match your vision "
         
     | 
| 47 | 
         
            +
                    "– and you'll see progress **and logs** live!"
         
     | 
| 48 | 
         
             
                )
         
     | 
| 49 | 
         
            +
             
     | 
| 50 | 
         
             
                with gr.Row():
         
     | 
| 51 | 
         
             
                    with gr.Column():
         
     | 
| 52 | 
         
             
                        image_input = gr.Image(type="pil", label="Upload Image")
         
     | 
| 53 | 
         
            +
                        prompt_input = gr.Textbox(label="Describe the vibe you want", placeholder="e.g. dreamy, vintage, vibrant…")
         
     | 
| 
         | 
|
| 
         | 
|
| 54 | 
         
             
                        submit_btn = gr.Button("Go!")
         
     | 
| 55 | 
         
             
                    with gr.Column():
         
     | 
| 56 | 
         
            +
                        streamed_image = gr.Image(label="Image Progress")
         
     | 
| 57 | 
         
            +
                        agent_logs = gr.Textbox(label="Agent Logs", lines=18, interactive=False)
         
     | 
| 
         | 
|
| 58 | 
         | 
| 59 | 
         
             
                submit_btn.click(
         
     | 
| 60 | 
         
             
                    process_image_with_agents,
         
     | 
| 61 | 
         
             
                    inputs=[image_input, prompt_input],
         
     | 
| 62 | 
         
            +
                    outputs=[streamed_image, agent_logs],
         
     | 
| 63 | 
         
             
                )
         
     | 
| 64 | 
         | 
| 65 | 
         
            +
                # Allow multiple users to queue without blocking streaming
         
     | 
| 66 | 
         
            +
                demo.queue()
         
     | 
| 67 | 
         
            +
             
     | 
| 68 | 
         
             
            if __name__ == "__main__":
         
     | 
| 69 | 
         
             
                demo.launch()
         
     |