import gradio as gr from src.agents.text_agent import TextAgent from src.agents.image_agent import ImageAgent from src.agents.coordinator_agent import CoordinatorAgent from PIL import Image async def process_content(text: str = None, image: Image.Image = None): text_result = None image_result = None # Process text if provided if text and text.strip(): try: text_agent = TextAgent(confidence_threshold=0.8) text_result = await text_agent.process(text) except Exception as e: return f"Error processing text: {str(e)}" # Process image if provided if image is not None: try: image_agent = ImageAgent(confidence_threshold=0.8) image_result = await image_agent.process(image) except Exception as e: return f"Error processing image: {str(e)}" # Coordinate results try: coordinator = CoordinatorAgent() #final_analysis = await coordinator.analyze_content(text_result, image_result) final_analysis = await coordinator.process({ 'text_result': text_result, 'image_result': image_result }) # Format the output output_parts = [] # Add original results if available if text_result: output_parts.append(f""" Text Analysis Results: --------------------- Text: {text_result['text']} Model Used: {text_result['model_used']} Label: {text_result['label']} Confidence: {text_result['confidence']:.2%} """) # if image_result: # output_parts.append(f""" # Image Analysis Results: # ---------------------- # Caption: {image_result['caption']} # Model Used: {image_result['model_used']} # Confidence: {image_result['confidence']:.2%} # """) if image_result: output_parts.append(f""" Image Analysis Results: ---------------------- Caption: {image_result['caption']} Model Used: {image_result['model_used']} Safety: {"Safe" if image_result['is_safe'] else "Potentially Unsafe"} Confidence: {image_result['confidence']:.2%} """) # Add correlation analysis if both text and image were processed if final_analysis["correlation"]: output_parts.append(f""" Correlation Analysis: ------------------- Correlation Level: {final_analysis["correlation"]["level"]} Correlation Score: {final_analysis["correlation"]["score"]:.2f} """) # Add final analysis output_parts.append(""" Combined Analysis: ----------------""") for analysis_point in final_analysis["analysis"]: output_parts.append(f"- {analysis_point}") return "\n".join(output_parts) except Exception as e: return f"Error in coordination: {str(e)}" def create_interface(): with gr.Blocks() as demo: gr.Markdown("# Content Analysis System") gr.Markdown("Upload an image and/or enter text for analysis. The system will analyze both individually and provide a combined analysis.") with gr.Row(): # Left column for text with gr.Column(): text_input = gr.Textbox( label="Enter text (max 50 words)", placeholder="Type your text here...", max_lines=3 ) # Right column for image with gr.Column(): image_input = gr.Image( type="pil", label="Upload Image" ) # Submit button submit_btn = gr.Button("Analyze Content") # Output area with increased height for detailed analysis output = gr.Textbox( label="Analysis Results", lines=10 ) # Click handler submit_btn.click( fn=process_content, inputs=[text_input, image_input], outputs=output ) return demo # Create and launch the interface demo = create_interface() # For HuggingFace Spaces deployment if __name__ == "__main__": demo.launch()