|
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 |
|
|
|
|
|
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)}" |
|
|
|
|
|
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)}" |
|
|
|
|
|
try: |
|
coordinator = CoordinatorAgent() |
|
|
|
final_analysis = await coordinator.process({ |
|
'text_result': text_result, |
|
'image_result': image_result |
|
}) |
|
|
|
output_parts = [] |
|
|
|
|
|
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']} |
|
Safety: {"Safe" if image_result['is_safe'] else "Potentially Unsafe"} |
|
Confidence: {image_result['confidence']:.2%} |
|
""") |
|
|
|
|
|
|
|
if final_analysis["correlation"]: |
|
output_parts.append(f""" |
|
Correlation Analysis: |
|
------------------- |
|
Correlation Level: {final_analysis["correlation"]["level"]} |
|
Correlation Score: {final_analysis["correlation"]["score"]:.2f} |
|
""") |
|
|
|
|
|
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(): |
|
|
|
with gr.Column(): |
|
text_input = gr.Textbox( |
|
label="Enter text (max 50 words)", |
|
placeholder="Type your text here...", |
|
max_lines=3 |
|
) |
|
|
|
|
|
with gr.Column(): |
|
image_input = gr.Image( |
|
type="pil", |
|
label="Upload Image" |
|
) |
|
|
|
|
|
submit_btn = gr.Button("Analyze Content") |
|
|
|
|
|
output = gr.Textbox( |
|
label="Analysis Results", |
|
lines=10 |
|
) |
|
|
|
|
|
submit_btn.click( |
|
fn=process_content, |
|
inputs=[text_input, image_input], |
|
outputs=output |
|
) |
|
|
|
return demo |
|
|
|
|
|
demo = create_interface() |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|