🤖 Multi-Agent AI System
Parallel AI Processing with Specialized Agents | Powered by Hugging Face Inference API
import gradio as gr from huggingface_hub import InferenceClient from concurrent.futures import ThreadPoolExecutor, as_completed import time from datetime import datetime import os # Use Hugging Face Inference API (no model loading needed!) # This is FREE and much faster! AGENT_CONFIGS = { "researcher": { "model": "mistralai/Mistral-7B-Instruct-v0.2", "role": "Research and gather information", "system_prompt": "You are a research agent specialized in gathering and analyzing information. Provide detailed, well-researched responses." }, "coder": { "model": "bigcode/starcoder2-15b", "role": "Generate and explain code", "system_prompt": "You are an expert programmer. Generate clean, efficient, well-commented code." }, "analyzer": { "model": "mistralai/Mistral-7B-Instruct-v0.2", "role": "Analyze data and provide insights", "system_prompt": "You are a data analyst. Provide clear insights and actionable recommendations." }, "writer": { "model": "mistralai/Mistral-7B-Instruct-v0.2", "role": "Create content and documentation", "system_prompt": "You are a technical writer. Create clear, professional documentation and content." } } class AgentSystem: def __init__(self): # No model loading! Using HF Inference API self.clients = {} self.executor = ThreadPoolExecutor(max_workers=4) # Initialize inference clients for each agent for agent_name in AGENT_CONFIGS.keys(): model = AGENT_CONFIGS[agent_name]["model"] self.clients[agent_name] = InferenceClient(model=model) print("✅ Agent system initialized with Inference API!") def generate_response(self, agent_name, task, max_tokens=300): """Generate response using HF Inference API""" try: config = AGENT_CONFIGS[agent_name] client = self.clients[agent_name] # Create prompt messages = [ { "role": "system", "content": config["system_prompt"] }, { "role": "user", "content": f"Task: {task}" } ] # Generate response response_text = "" for message in client.chat_completion( messages=messages, max_tokens=max_tokens, temperature=0.7, stream=True ): if hasattr(message.choices[0].delta, 'content'): response_text += message.choices[0].delta.content return { "agent": agent_name, "role": config["role"], "response": response_text.strip(), "status": "success" } except Exception as e: return { "agent": agent_name, "role": AGENT_CONFIGS[agent_name]["role"], "response": f"Error: {str(e)}", "status": "error" } def run_agents_parallel(self, task, selected_agents, max_tokens=300): """Run multiple agents in parallel""" start_time = time.time() futures = {} results = [] # Submit tasks to thread pool for agent_name in selected_agents: future = self.executor.submit( self.generate_response, agent_name, task, max_tokens ) futures[future] = agent_name # Collect results as they complete for future in as_completed(futures): agent_name = futures[future] try: result = future.result(timeout=30) # 30 second timeout per agent result["time_taken"] = round(time.time() - start_time, 2) results.append(result) except Exception as e: results.append({ "agent": agent_name, "role": AGENT_CONFIGS[agent_name]["role"], "response": f"Timeout or error: {str(e)}", "status": "error", "time_taken": round(time.time() - start_time, 2) }) total_time = round(time.time() - start_time, 2) return results, total_time # Initialize the agent system print("🚀 Initializing AI Agent System...") agent_system = AgentSystem() print("✅ System ready!") def process_task(task, researcher, coder, analyzer, writer, max_tokens, progress=gr.Progress()): """Process task with selected agents""" if not task.strip(): return "⚠️ Please enter a task!", "", "" # Determine which agents to use selected_agents = [] if researcher: selected_agents.append("researcher") if coder: selected_agents.append("coder") if analyzer: selected_agents.append("analyzer") if writer: selected_agents.append("writer") if not selected_agents: return "⚠️ Please select at least one agent!", "", "" progress(0, desc="Starting agents...") # Run agents in parallel results, total_time = agent_system.run_agents_parallel(task, selected_agents, max_tokens) progress(1, desc="Complete!") # Format output output = f"# 🤖 AI Agent System Results\n\n" output += f"**Task:** {task}\n\n" output += f"**Agents Used:** {len(selected_agents)} agents running in parallel\n\n" output += f"**Total Time:** {total_time}s\n\n" output += "---\n\n" for idx, result in enumerate(results, 1): status_emoji = "✅" if result["status"] == "success" else "❌" output += f"## {status_emoji} Agent {idx}: {result['agent'].upper()}\n\n" output += f"**Role:** {result['role']}\n\n" output += f"**Response:**\n\n{result['response']}\n\n" output += f"*⏱️ Completed in {result['time_taken']}s*\n\n" output += "---\n\n" # Create summary stats success_count = sum(1 for r in results if r["status"] == "success") stats = f"""📊 **Execution Stats** - Total Agents: {len(selected_agents)} - Successful: {success_count} - Failed: {len(selected_agents) - success_count} - Total Time: {total_time}s - Average per Agent: {round(total_time / len(selected_agents), 2)}s """ # Detailed JSON for download details = { "task": task, "agents_used": selected_agents, "total_time": total_time, "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "results": results } import json json_output = json.dumps(details, indent=2) return output, stats, json_output # Create Gradio Interface custom_css = """ .gradio-container { font-family: 'Inter', sans-serif; } .main-header { text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px; } """ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="AI Agent System") as demo: gr.HTML("""
Parallel AI Processing with Specialized Agents | Powered by Hugging Face Inference API