faizee07 commited on
Commit
432e2e3
Β·
verified Β·
1 Parent(s): 16c3a1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +203 -145
app.py CHANGED
@@ -1,102 +1,82 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
- import torch
4
  from concurrent.futures import ThreadPoolExecutor, as_completed
5
- import json
6
  import time
7
  from datetime import datetime
 
 
 
 
8
 
9
- # Configuration for different agent types
10
  AGENT_CONFIGS = {
11
  "researcher": {
12
- "model": "HuggingFaceH4/zephyr-7b-beta",
13
  "role": "Research and gather information",
14
- "prompt_template": "You are a research agent. Analyze and provide detailed information about: {task}"
15
  },
16
  "coder": {
17
- "model": "Salesforce/codegen-350M-mono",
18
  "role": "Generate and explain code",
19
- "prompt_template": "Generate Python code for: {task}"
20
  },
21
  "analyzer": {
22
- "model": "HuggingFaceH4/zephyr-7b-beta",
23
  "role": "Analyze data and provide insights",
24
- "prompt_template": "Analyze the following and provide insights: {task}"
25
  },
26
  "writer": {
27
- "model": "HuggingFaceH4/zephyr-7b-beta",
28
  "role": "Create content and documentation",
29
- "prompt_template": "Write professional content about: {task}"
30
  }
31
  }
32
 
33
  class AgentSystem:
34
  def __init__(self):
35
- self.models = {}
36
- self.tokenizers = {}
37
  self.executor = ThreadPoolExecutor(max_workers=4)
38
- self.device = "cuda" if torch.cuda.is_available() else "cpu"
39
- print(f"Using device: {self.device}")
40
 
41
- def load_model(self, agent_name, model_name):
42
- """Load model for specific agent"""
43
- if agent_name not in self.models:
44
- print(f"Loading {agent_name} model: {model_name}")
45
- try:
46
- self.tokenizers[agent_name] = AutoTokenizer.from_pretrained(model_name)
47
- self.models[agent_name] = AutoModelForCausalLM.from_pretrained(
48
- model_name,
49
- torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
50
- low_cpu_mem_usage=True,
51
- device_map="auto" if self.device == "cuda" else None
52
- )
53
- print(f"{agent_name} model loaded successfully!")
54
- except Exception as e:
55
- print(f"Error loading {agent_name} model: {e}")
56
- # Fallback to smaller model
57
- print(f"Falling back to distilgpt2 for {agent_name}")
58
- self.tokenizers[agent_name] = AutoTokenizer.from_pretrained("distilgpt2")
59
- self.models[agent_name] = AutoModelForCausalLM.from_pretrained("distilgpt2")
60
 
61
- def generate_response(self, agent_name, prompt, max_length=200):
62
- """Generate response for a specific agent"""
63
  try:
64
  config = AGENT_CONFIGS[agent_name]
65
- model_name = config["model"]
66
 
67
- # Load model if not already loaded
68
- if agent_name not in self.models:
69
- self.load_model(agent_name, model_name)
 
 
 
 
 
 
 
 
70
 
71
- tokenizer = self.tokenizers[agent_name]
72
- model = self.models[agent_name]
73
-
74
- # Format prompt
75
- formatted_prompt = config["prompt_template"].format(task=prompt)
76
-
77
- # Tokenize
78
- inputs = tokenizer(formatted_prompt, return_tensors="pt", truncation=True, max_length=512)
79
-
80
- if self.device == "cuda":
81
- inputs = inputs.to("cuda")
82
-
83
- # Generate
84
- with torch.no_grad():
85
- outputs = model.generate(
86
- inputs.input_ids,
87
- max_length=max_length,
88
- temperature=0.7,
89
- top_p=0.9,
90
- do_sample=True,
91
- pad_token_id=tokenizer.eos_token_id
92
- )
93
-
94
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
95
 
96
  return {
97
  "agent": agent_name,
98
  "role": config["role"],
99
- "response": response,
100
  "status": "success"
101
  }
102
 
@@ -108,7 +88,7 @@ class AgentSystem:
108
  "status": "error"
109
  }
110
 
111
- def run_agents_parallel(self, task, selected_agents, max_length=200):
112
  """Run multiple agents in parallel"""
113
  start_time = time.time()
114
  futures = {}
@@ -120,7 +100,7 @@ class AgentSystem:
120
  self.generate_response,
121
  agent_name,
122
  task,
123
- max_length
124
  )
125
  futures[future] = agent_name
126
 
@@ -128,14 +108,14 @@ class AgentSystem:
128
  for future in as_completed(futures):
129
  agent_name = futures[future]
130
  try:
131
- result = future.result()
132
  result["time_taken"] = round(time.time() - start_time, 2)
133
  results.append(result)
134
  except Exception as e:
135
  results.append({
136
  "agent": agent_name,
137
  "role": AGENT_CONFIGS[agent_name]["role"],
138
- "response": f"Failed: {str(e)}",
139
  "status": "error",
140
  "time_taken": round(time.time() - start_time, 2)
141
  })
@@ -144,13 +124,14 @@ class AgentSystem:
144
  return results, total_time
145
 
146
  # Initialize the agent system
147
- print("Initializing AI Agent System...")
148
  agent_system = AgentSystem()
 
149
 
150
- def process_task(task, researcher, coder, analyzer, writer, max_length):
151
  """Process task with selected agents"""
152
  if not task.strip():
153
- return "Please enter a task!", ""
154
 
155
  # Determine which agents to use
156
  selected_agents = []
@@ -164,10 +145,14 @@ def process_task(task, researcher, coder, analyzer, writer, max_length):
164
  selected_agents.append("writer")
165
 
166
  if not selected_agents:
167
- return "Please select at least one agent!", ""
 
 
168
 
169
  # Run agents in parallel
170
- results, total_time = agent_system.run_agents_parallel(task, selected_agents, max_length)
 
 
171
 
172
  # Format output
173
  output = f"# πŸ€– AI Agent System Results\n\n"
@@ -176,119 +161,192 @@ def process_task(task, researcher, coder, analyzer, writer, max_length):
176
  output += f"**Total Time:** {total_time}s\n\n"
177
  output += "---\n\n"
178
 
179
- for result in results:
180
  status_emoji = "βœ…" if result["status"] == "success" else "❌"
181
- output += f"## {status_emoji} {result['agent'].upper()} Agent\n"
182
  output += f"**Role:** {result['role']}\n\n"
183
- output += f"**Response:**\n```\n{result['response']}\n```\n\n"
184
- output += f"*Completed in {result['time_taken']}s*\n\n"
185
  output += "---\n\n"
186
 
187
- # Create summary
188
- summary = {
 
 
 
 
 
 
 
 
 
 
189
  "task": task,
190
- "agents_used": len(selected_agents),
191
  "total_time": total_time,
192
- "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 
193
  }
194
 
195
- return output, json.dumps(summary, indent=2)
 
 
 
196
 
197
  # Create Gradio Interface
198
- with gr.Blocks(theme=gr.themes.Soft(), title="AI Agent System") as demo:
199
- gr.Markdown(
200
- """
201
- # πŸ€– Full-Stack AI Agent System
202
-
203
- **Parallel AI Processing with Multiple Specialized Agents**
204
-
205
- This system runs multiple AI agents simultaneously to process your tasks faster!
206
- Each agent specializes in different areas and works in parallel.
207
- """
208
- )
 
 
 
 
 
 
 
 
 
 
209
 
210
  with gr.Row():
211
  with gr.Column(scale=1):
212
- gr.Markdown("### πŸ“‹ Task Input")
 
213
  task_input = gr.Textbox(
214
- label="Enter Your Task",
215
- placeholder="Example: Create a Python web scraper for news articles",
216
- lines=4
217
  )
218
 
219
- gr.Markdown("### 🎯 Select Agents")
220
- researcher_check = gr.Checkbox(label="πŸ” Researcher Agent", value=True, info="Research and gather information")
221
- coder_check = gr.Checkbox(label="πŸ’» Coder Agent", value=True, info="Generate and explain code")
222
- analyzer_check = gr.Checkbox(label="πŸ“Š Analyzer Agent", value=True, info="Analyze and provide insights")
223
- writer_check = gr.Checkbox(label="✍️ Writer Agent", value=True, info="Create documentation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
- max_length = gr.Slider(
226
  minimum=100,
227
  maximum=500,
228
- value=200,
229
  step=50,
230
- label="Max Response Length",
231
- info="Tokens per agent"
232
  )
233
 
234
- process_btn = gr.Button("πŸš€ Run Agents in Parallel", variant="primary", size="lg")
 
 
 
 
235
 
236
- gr.Markdown(
237
- """
238
- ### πŸ’‘ Tips
239
- - Select multiple agents for comprehensive results
240
- - Agents run simultaneously for faster processing
241
  - Each agent brings unique expertise
242
- """
243
- )
244
 
245
  with gr.Column(scale=2):
246
- gr.Markdown("### πŸ“€ Agent Outputs")
247
- output_display = gr.Markdown(label="Results")
 
 
 
 
248
 
249
- with gr.Accordion("πŸ“Š Execution Summary", open=False):
250
- summary_json = gr.Code(label="JSON Summary", language="json")
 
 
 
 
 
 
 
 
 
251
 
252
- gr.Markdown("### πŸ“š Example Tasks")
253
  gr.Examples(
254
  examples=[
255
- ["Create a REST API for user authentication"],
256
- ["Build a machine learning model for sentiment analysis"],
257
- ["Design a database schema for an e-commerce platform"],
258
- ["Write a technical blog post about microservices"],
259
- ["Develop a real-time chat application"]
260
  ],
261
  inputs=task_input
262
  )
263
 
264
- gr.Markdown(
265
- """
266
  ---
267
 
268
  ## πŸ—οΈ System Architecture
269
 
270
- - **Parallel Processing**: All agents run simultaneously using ThreadPoolExecutor
271
- - **Free Models**: Using Hugging Face hosted models (Zephyr-7B, CodeGen)
272
- - **Specialized Agents**: Each agent has a specific role and expertise
273
- - **Fault Tolerant**: Continues even if one agent fails
274
 
275
- ## πŸ”§ Technology Stack
 
 
 
276
 
277
- - **Frontend**: Gradio
278
- - **Backend**: Python + Transformers
279
- - **Models**: Hugging Face free models
280
- - **Concurrency**: ThreadPoolExecutor for parallel processing
281
- """
282
- )
 
 
 
 
283
 
284
- # Connect button to processing function
285
  process_btn.click(
286
  fn=process_task,
287
- inputs=[task_input, researcher_check, coder_check, analyzer_check, writer_check, max_length],
288
- outputs=[output_display, summary_json]
 
 
 
 
 
 
 
289
  )
290
 
291
- # Launch
292
  if __name__ == "__main__":
293
- demo.queue() # Enable queuing for better performance
294
- demo.launch()
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
3
  from concurrent.futures import ThreadPoolExecutor, as_completed
 
4
  import time
5
  from datetime import datetime
6
+ import os
7
+
8
+ # Use Hugging Face Inference API (no model loading needed!)
9
+ # This is FREE and much faster!
10
 
 
11
  AGENT_CONFIGS = {
12
  "researcher": {
13
+ "model": "mistralai/Mistral-7B-Instruct-v0.2",
14
  "role": "Research and gather information",
15
+ "system_prompt": "You are a research agent specialized in gathering and analyzing information. Provide detailed, well-researched responses."
16
  },
17
  "coder": {
18
+ "model": "bigcode/starcoder2-15b",
19
  "role": "Generate and explain code",
20
+ "system_prompt": "You are an expert programmer. Generate clean, efficient, well-commented code."
21
  },
22
  "analyzer": {
23
+ "model": "mistralai/Mistral-7B-Instruct-v0.2",
24
  "role": "Analyze data and provide insights",
25
+ "system_prompt": "You are a data analyst. Provide clear insights and actionable recommendations."
26
  },
27
  "writer": {
28
+ "model": "mistralai/Mistral-7B-Instruct-v0.2",
29
  "role": "Create content and documentation",
30
+ "system_prompt": "You are a technical writer. Create clear, professional documentation and content."
31
  }
32
  }
33
 
34
  class AgentSystem:
35
  def __init__(self):
36
+ # No model loading! Using HF Inference API
37
+ self.clients = {}
38
  self.executor = ThreadPoolExecutor(max_workers=4)
 
 
39
 
40
+ # Initialize inference clients for each agent
41
+ for agent_name in AGENT_CONFIGS.keys():
42
+ model = AGENT_CONFIGS[agent_name]["model"]
43
+ self.clients[agent_name] = InferenceClient(model=model)
44
+
45
+ print("βœ… Agent system initialized with Inference API!")
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ def generate_response(self, agent_name, task, max_tokens=300):
48
+ """Generate response using HF Inference API"""
49
  try:
50
  config = AGENT_CONFIGS[agent_name]
51
+ client = self.clients[agent_name]
52
 
53
+ # Create prompt
54
+ messages = [
55
+ {
56
+ "role": "system",
57
+ "content": config["system_prompt"]
58
+ },
59
+ {
60
+ "role": "user",
61
+ "content": f"Task: {task}"
62
+ }
63
+ ]
64
 
65
+ # Generate response
66
+ response_text = ""
67
+ for message in client.chat_completion(
68
+ messages=messages,
69
+ max_tokens=max_tokens,
70
+ temperature=0.7,
71
+ stream=True
72
+ ):
73
+ if hasattr(message.choices[0].delta, 'content'):
74
+ response_text += message.choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  return {
77
  "agent": agent_name,
78
  "role": config["role"],
79
+ "response": response_text.strip(),
80
  "status": "success"
81
  }
82
 
 
88
  "status": "error"
89
  }
90
 
91
+ def run_agents_parallel(self, task, selected_agents, max_tokens=300):
92
  """Run multiple agents in parallel"""
93
  start_time = time.time()
94
  futures = {}
 
100
  self.generate_response,
101
  agent_name,
102
  task,
103
+ max_tokens
104
  )
105
  futures[future] = agent_name
106
 
 
108
  for future in as_completed(futures):
109
  agent_name = futures[future]
110
  try:
111
+ result = future.result(timeout=30) # 30 second timeout per agent
112
  result["time_taken"] = round(time.time() - start_time, 2)
113
  results.append(result)
114
  except Exception as e:
115
  results.append({
116
  "agent": agent_name,
117
  "role": AGENT_CONFIGS[agent_name]["role"],
118
+ "response": f"Timeout or error: {str(e)}",
119
  "status": "error",
120
  "time_taken": round(time.time() - start_time, 2)
121
  })
 
124
  return results, total_time
125
 
126
  # Initialize the agent system
127
+ print("πŸš€ Initializing AI Agent System...")
128
  agent_system = AgentSystem()
129
+ print("βœ… System ready!")
130
 
131
+ def process_task(task, researcher, coder, analyzer, writer, max_tokens, progress=gr.Progress()):
132
  """Process task with selected agents"""
133
  if not task.strip():
134
+ return "⚠️ Please enter a task!", "", ""
135
 
136
  # Determine which agents to use
137
  selected_agents = []
 
145
  selected_agents.append("writer")
146
 
147
  if not selected_agents:
148
+ return "⚠️ Please select at least one agent!", "", ""
149
+
150
+ progress(0, desc="Starting agents...")
151
 
152
  # Run agents in parallel
153
+ results, total_time = agent_system.run_agents_parallel(task, selected_agents, max_tokens)
154
+
155
+ progress(1, desc="Complete!")
156
 
157
  # Format output
158
  output = f"# πŸ€– AI Agent System Results\n\n"
 
161
  output += f"**Total Time:** {total_time}s\n\n"
162
  output += "---\n\n"
163
 
164
+ for idx, result in enumerate(results, 1):
165
  status_emoji = "βœ…" if result["status"] == "success" else "❌"
166
+ output += f"## {status_emoji} Agent {idx}: {result['agent'].upper()}\n\n"
167
  output += f"**Role:** {result['role']}\n\n"
168
+ output += f"**Response:**\n\n{result['response']}\n\n"
169
+ output += f"*⏱️ Completed in {result['time_taken']}s*\n\n"
170
  output += "---\n\n"
171
 
172
+ # Create summary stats
173
+ success_count = sum(1 for r in results if r["status"] == "success")
174
+ stats = f"""πŸ“Š **Execution Stats**
175
+ - Total Agents: {len(selected_agents)}
176
+ - Successful: {success_count}
177
+ - Failed: {len(selected_agents) - success_count}
178
+ - Total Time: {total_time}s
179
+ - Average per Agent: {round(total_time / len(selected_agents), 2)}s
180
+ """
181
+
182
+ # Detailed JSON for download
183
+ details = {
184
  "task": task,
185
+ "agents_used": selected_agents,
186
  "total_time": total_time,
187
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
188
+ "results": results
189
  }
190
 
191
+ import json
192
+ json_output = json.dumps(details, indent=2)
193
+
194
+ return output, stats, json_output
195
 
196
  # Create Gradio Interface
197
+ custom_css = """
198
+ .gradio-container {
199
+ font-family: 'Inter', sans-serif;
200
+ }
201
+ .main-header {
202
+ text-align: center;
203
+ padding: 20px;
204
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
205
+ color: white;
206
+ border-radius: 10px;
207
+ margin-bottom: 20px;
208
+ }
209
+ """
210
+
211
+ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="AI Agent System") as demo:
212
+ gr.HTML("""
213
+ <div class="main-header">
214
+ <h1>πŸ€– Multi-Agent AI System</h1>
215
+ <p>Parallel AI Processing with Specialized Agents | Powered by Hugging Face Inference API</p>
216
+ </div>
217
+ """)
218
 
219
  with gr.Row():
220
  with gr.Column(scale=1):
221
+ gr.Markdown("### πŸ“ Task Configuration")
222
+
223
  task_input = gr.Textbox(
224
+ label="What do you want the agents to work on?",
225
+ placeholder="Example: Build a user authentication system with JWT tokens",
226
+ lines=5
227
  )
228
 
229
+ gr.Markdown("### 🎯 Select Your Team")
230
+
231
+ with gr.Group():
232
+ researcher_check = gr.Checkbox(
233
+ label="πŸ” Researcher Agent",
234
+ value=True,
235
+ info="Gathers information and best practices"
236
+ )
237
+ coder_check = gr.Checkbox(
238
+ label="πŸ’» Coder Agent",
239
+ value=True,
240
+ info="Writes production-ready code"
241
+ )
242
+ analyzer_check = gr.Checkbox(
243
+ label="πŸ“Š Analyzer Agent",
244
+ value=True,
245
+ info="Provides insights and recommendations"
246
+ )
247
+ writer_check = gr.Checkbox(
248
+ label="✍️ Writer Agent",
249
+ value=True,
250
+ info="Creates documentation"
251
+ )
252
+
253
+ gr.Markdown("### βš™οΈ Settings")
254
 
255
+ max_tokens = gr.Slider(
256
  minimum=100,
257
  maximum=500,
258
+ value=300,
259
  step=50,
260
+ label="Response Length",
261
+ info="Tokens per agent response"
262
  )
263
 
264
+ process_btn = gr.Button(
265
+ "πŸš€ Deploy Agents",
266
+ variant="primary",
267
+ size="lg"
268
+ )
269
 
270
+ gr.Markdown("""
271
+ ### πŸ’‘ Pro Tips
272
+ - Use all 4 agents for comprehensive results
273
+ - Agents run simultaneously = 3-4x faster!
 
274
  - Each agent brings unique expertise
275
+ - No model downloads = instant startup
276
+ """)
277
 
278
  with gr.Column(scale=2):
279
+ gr.Markdown("### πŸ“Š Results Dashboard")
280
+
281
+ output_display = gr.Markdown(
282
+ value="*Results will appear here after running agents...*",
283
+ label="Agent Outputs"
284
+ )
285
 
286
+ with gr.Accordion("πŸ“ˆ Execution Statistics", open=True):
287
+ stats_display = gr.Markdown(value="*No data yet*")
288
+
289
+ with gr.Accordion("πŸ’Ύ Download Results (JSON)", open=False):
290
+ json_output = gr.Code(
291
+ label="Complete Results",
292
+ language="json",
293
+ lines=10
294
+ )
295
+
296
+ gr.Markdown("### πŸ“š Quick Start Examples")
297
 
 
298
  gr.Examples(
299
  examples=[
300
+ ["Create a REST API for a todo list application with authentication"],
301
+ ["Build a machine learning pipeline for image classification"],
302
+ ["Design a microservices architecture for an e-commerce platform"],
303
+ ["Develop a real-time chat application using WebSockets"],
304
+ ["Create a data visualization dashboard for sales analytics"],
305
  ],
306
  inputs=task_input
307
  )
308
 
309
+ gr.Markdown("""
 
310
  ---
311
 
312
  ## πŸ—οΈ System Architecture
313
 
314
+ **How It Works:**
 
 
 
315
 
316
+ 1. **Task Distribution** β†’ Your task is sent to selected agents
317
+ 2. **Parallel Processing** β†’ All agents work simultaneously (not sequential!)
318
+ 3. **Smart Aggregation** β†’ Results are collected as they complete
319
+ 4. **Instant Results** β†’ See output from each agent in real-time
320
 
321
+ **Technology:**
322
+ - ⚑ Hugging Face Inference API (serverless, no model loading)
323
+ - πŸ”„ ThreadPoolExecutor for true parallelism
324
+ - πŸš€ Free tier compatible
325
+ - πŸ“Š Real-time progress tracking
326
+
327
+ **Models Used:**
328
+ - Mistral-7B-Instruct (Researcher, Analyzer, Writer)
329
+ - StarCoder2-15B (Coder)
330
+ """)
331
 
332
+ # Connect button
333
  process_btn.click(
334
  fn=process_task,
335
+ inputs=[
336
+ task_input,
337
+ researcher_check,
338
+ coder_check,
339
+ analyzer_check,
340
+ writer_check,
341
+ max_tokens
342
+ ],
343
+ outputs=[output_display, stats_display, json_output]
344
  )
345
 
346
+ # Launch with optimized settings
347
  if __name__ == "__main__":
348
+ demo.queue(max_size=20) # Handle multiple users
349
+ demo.launch(
350
+ show_error=True,
351
+ share=False
352
+ )