File size: 28,942 Bytes
4af1b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
4cf9235
3cebaf0
4cf9235
 
 
 
4af1b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cc5961
4af1b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cc5961
4af1b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cf9235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4af1b1b
 
 
ec937b1
 
 
 
 
 
4cf9235
4af1b1b
4cf9235
 
 
 
 
4af1b1b
 
 
ec937b1
 
 
 
 
 
3cebaf0
4af1b1b
 
 
 
 
 
 
 
ec8bf9a
4af1b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cf9235
4af1b1b
 
ec8bf9a
4af1b1b
 
 
 
4cf9235
4af1b1b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import gradio as gr
import requests, asyncio
import os, sys, subprocess, json, time, re, gc, shutil, random, urllib3
from gradio import ChatMessage
from dataclasses import dataclass, asdict
from markdownify import markdownify as markdownify
from huggingface_hub import login, InferenceClient, AsyncInferenceClient, HfApi, hf_hub_download
import gradio_client as g_client
from langchain.memory import ConversationBufferMemory
import bs4
import soundfile as sf
from transformers import pipeline
from transformers.agents import ToolCollection, Toolbox, ReactCodeAgent, CodeAgent, ReactAgent, ReactJsonAgent, stream_to_gradio, HfApiEngine, DuckDuckGoSearchTool, ManagedAgent, PythonInterpreterTool, load_tool, TransformersEngine, agent_types, FinalAnswerTool
from smolagents import UserInputTool
import mimetypes
import sentence_transformers
from typing import Optional
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
from smolagents.agents import ActionStep, MultiStepAgent
from smolagents.memory import MemoryStep
from transformers.agents import Tool as Atool
from transformers.agents.default_tools import Tool as Ragtool
from smolagents import tool as Smoltool
from smolagents import CodeAgent as SmolcodeAgent
from smolagents import MultiStepAgent as SmolmultistepAgent
from smolagents import ToolCallingAgent as SmoltoolcallingAgent
from smolagents import WebSearchTool, LiteLLMModel
from smolagents import InferenceClientModel as SmolInfCliModel
from smolagents import Tool as Stool
from smolagents import TransformersModel, ApiModel, VLLMModel, MLXModel, LiteLLMModel, LiteLLMRouterModel, OpenAIServerModel, AzureOpenAIServerModel, AmazonBedrockServerModel
import datasets
import spaces
import tqdm
from gtts import gTTS
from duckduckgo_search import DDGS
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
##from llama_cpp import Llama
from langchain_community.vectorstores import FAISS
from smolagents import default_tools
from langchain.docstore.document import Document
from typing import Generator
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_core.vectorstores import VectorStore
HF_TOKEN=os.environ.get('HF_TOKEN')
login(token=HF_TOKEN)
loots=[]
soots=[]
messages=[]
custom_css="""
#papa {
  width: 50%;
}
#popa {
  width: 50%;
}
.gradio-container {
  animation: growShrink 3s 1 forwards;
}
@keyframes growShrink {
  from {
    transform: scale(0.1);
  }
  to {
    transform: scale(1.0);
  }
}
"""
head_js="""
<script>
document.getElementById('papa').setAttribute('style', 'width: 50%;');
document.getElementById('popa').setAttribute('style', 'width: 50%;');
</script>
"""
engines=["InferenceClientModel","ApiModel","LiteLLMModel","AzureOpenAIServerModel","AmazonBedrockServerModel","MLXModel","LiteLLMRouterModel","OpenAIServerModel","TransformersEngine","VLLMModel","TransformersModel","HfApiModel"]
all_tasks=[]
moss=[]
mass=[]
def search_ducky(query):
    with DDGS() as ddgs:
        results = list(ddgs.text(query, max_results=10))
        content = ''
        if results:
            for result in results:
                content += result['body']
                return content
knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
source_docs = [Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]}) for doc in knowledge_base]
docs_processed = RecursiveCharacterTextSplitter(chunk_size=500).split_documents(source_docs)[:1000]
embedding_model = HuggingFaceEmbeddings(model_name="thenlper/gte-small")
vectordb = FAISS.from_documents(documents=docs_processed, embedding=embedding_model)
all_sources = list(set([doc.metadata["source"] for doc in docs_processed]))
print(all_sources)
class RetrieverTool(Ragtool):
    name = "retriever"
    description = "Retrieves some documents from the knowledge base that have the closest embeddings to the input query."
    inputs = {
        "query": {
            "type": "string",
            "description": "The query to perform. This should be semantically close to your target documents. Use the affirmative form rather than a question.",
        },
        "source": {
            "type": "string", 
            "description": ""
        },
    }
    output_type = "string"
    
    def __init__(self, vectordb: VectorStore, all_sources: str, **kwargs):
        super().__init__(**kwargs)
        self.vectordb = vectordb
        self.inputs["source"]["description"] = (f"The source of the documents to search, as a str representation of a list. Possible values in the list are: {all_sources}. If this argument is not provided, all sources will be searched.")

    def forward(self, query: str, source: str = None) -> str:
        assert isinstance(query, str), "Your search query must be a string"

        if source:
            if isinstance(source, str) and "[" not in str(source): # if the source is not representing a list
                source = [source]
            source = json.loads(str(source).replace("'", '"'))

        docs = self.vectordb.similarity_search(query, filter=({"source": source} if source else None), k=3)

        if len(docs) == 0:
            return "No documents found with this filtering. Try removing the source filter."
        return "Retrieved documents:\n\n" + "\n===Document===\n".join([doc.page_content for doc in docs])
retriever_tool = RetrieverTool(vectordb=vectordb, all_sources=all_sources)
memory = ConversationBufferMemory(memory_key="chat_history")
def get_hf_tasks(all_tasks):
    url = "https://huggingface.co/api/tasks"
    response = requests.get(url)
    tasks = response.json()
    task_list = []
    for task_type in tasks:
        task_list.append(task_type)
    all_tasks.extend(task_list)
    return all_tasks
get_hf_tasks(all_tasks)
def fetch_gradio_spaces():
    response = requests.get("https://huggingface.co/api/spaces?runtime=zero-a10g&sdk=gradio&sort=trendingScore&limit=10000")
    data = response.json()
    gradio_spaces = [space['id'] for space in data if space.get('sdk') == "gradio" and space['runtime']['hardware']['current'] != "cpu-basic" and space['runtime']['hardware']['current'] != None]
    return gradio_spaces
gradio_list=fetch_gradio_spaces()

@Smoltool
def visit_webpage(url: str) -> str:
    """Visits a webpage at the given URL and returns its content as a markdown string.

    Args:
        url: The URL of the webpage to visit.

    Returns:
        The content of the webpage converted to Markdown, or an error message if the request fails.
    """
    try:
        response = requests.get(url)
        response.raise_for_status()

        markdown_content = markdownify(response.text).strip()
        markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
        return markdown_content
    except RequestException as e:
        return f"Error fetching the webpage: {str(e)}"
    except Exception as e:
        return f"An unexpected error occurred: {str(e)}"

def add_task_tools():
    for tsk in all_tasks:
        tsk_tool=load_tool(tsk)
        loots.append(tsk_tool)
def add_space_tools():
    for sptool in gradio_list[:55]:
        try:
            nmo=str(re.sub(r"[-./]", "_", sptool))
            deco=str(re.sub(r"[-./]", " ", sptool))
            nw_tool = Stool.from_space(space_id=sptool, name=nmo, description=deco)
            soots.append(nw_tool)
            aw_tool=Atool.from_space(space_id=sptool, name=nmo, description=deco)
            loots.append(aw_tool)
        except IndexError:
            print(f"Skipping {sptool}: No API endpoint found.")
        except Exception as e:
            print(f"Error loading {sptool}: {e}")
add_space_tools()
print(loots)
##model="Qwen/Qwen2.5-Coder-32B-Instruct"
##llm_engine = HfApiEngine(model=model, token=HF_TOKEN, max_tokens=300, timeout=120)
##smodol = SmolInfCliModel(model_id=model)
#####llm = Llama(model_path=hf_hub_download(repo_id="bartowski/DeepSeek-R1-Distill-Qwen-1.5B-GGUF", filename="DeepSeek-R1-Distill-Qwen-1.5B-Q4_0.gguf", local_dir='~/app/Downloads'), n_ctx=512, seed=42,)
##def llama_cpp_engine(prompt=None,messages=None,max_tokens=256,temperature=0.7,stop=None,stream=False,functions=None):
#####    if messages:
#####        kwargs = {"messages": messages,"max_tokens": max_tokens,"temperature": temperature,"stop": stop,"stream": stream,}
#####        if functions:
#####            kwargs["functions"] = functions
#####        completion = llm.create_chat_completion(**kwargs)
#####    else:
#####        kwargs = {"prompt": prompt,"max_tokens": max_tokens,"temperature": temperature,"stop": stop,"stream": stream,}
#####        completion = llm.create_completion(**kwargs)
#####
#####    if stream:
#####
#####        for chunk in completion:
#####            if "choices" in chunk and chunk["choices"]:
#####                yield chunk["choices"][0]["text"]
#####    else:
#####
#####        if "choices" in completion and completion["choices"]:
#####            return completion["choices"][0]["text"]
#####        return ""



async def bouge(ma_tkens):
    client = AsyncInferenceClient(provider="nebius",api_key=HF_TOKEN,)
##        base_url="https://api.studio.nebius.com/v1",

    stream = await client.chat.completions.create(model="Qwen/Qwen2.5-Coder-32B-Instruct",messages=messages,max_tokens=ma_tkens,stream=True,)

    async for chunk in stream:
        yield chunk.choices.delta.content

def engine_llm(eng_name,moodl,provdr,mx_tkens):
    if eng_name=="InferenceClientModel":
        eng_smml=SmolInfCliModel(model_id=moodl,provider=provdr,max_tokens=mx_tkens,token=HF_TOKEN)
    elif eng_name=="ApiModel":
        eng_smml=ApiModel(model_id=moodl,client=asyncio.run(bouge(mx_tkens)))
    elif eng_name=="LiteLLMModel":
        eng_smml=LiteLLMModel()
    elif eng_name=="AzureOpenAIServerModel":
        eng_smml=AzureOpenAIServerModel()
    elif eng_name=="AmazonBedrockServerModel":
        eng_smml=AmazonBedrockServerModel()
    elif eng_name=="MLXModel":
        eng_smml=MLXModel()
    elif eng_name=="LiteLLMRouterModel":
        eng_smml=LiteLLMRouterModel()
    elif eng_name=="OpenAIServerModel":
        eng_smml=OpenAIServerModel()
    elif eng_name=="VLLMModel":
        eng_smml=VLLMModel()
    elif eng_name=="TransformersEngine":
        eng_smml=TransformersEngine(pipeline=pipeline("text-generation",torch_dtype=torch.bfloat16,device="cpu"))
    elif eng_name=="TransformersModel":
        eng_smml=TransformersModel(model_id="Qwen/Qwen2.5-Coder-1.5B-Instruct",device="cpu",max_new_tokens=mx_tkens,)
#####    elif eng_name=="LlamaCppModel":
#####        eng_smml=llama_cpp_engine(prompt=None,messages=messages,max_tokens=mx_tkens,temperature=0.7,stop=None,stream=True,functions=None)
    elif eng_name=="HfApiEngine":
        eng_smml=HfApiEngine(model=model, token=HF_TOKEN, max_tokens=mx_tkens, timeout=120)
    else:
        return None
    return eng_smml

selected_tasks = all_tasks[:8]
agent_swarm_array = []
def create_agent_swarm(ngine, nugt, skats):
    cos = int(int(len(loots)) / int(len(skats)))
    for ido, tsk_type in enumerate(skats):
        sart = ido * cos
        ond = sart + cos
        matched_tools=[]
        decs=""
        words = re.findall(r'\w+', tsk_type.lower())
        patterns = [re.compile(r'\b' + re.escape(word) + r'\b', re.IGNORECASE) for word in words]
        for kool in loots:
            text = f"{kool.name} {kool.description}".lower()
            decs+=text
            if all(pattern.search(text) for pattern in patterns):
                matched_tools.append(kool)
        matched_tools = list(set(matched_tools))
        agent_two = CodeAgent(tools=[retriever_tool, *matched_tools], llm_engine=ngine, additional_authorized_imports=['requests', 'bs4', 'json', 'time', 're', 'g_client', 'urllib3', 'gTTS',], add_base_tools=True, max_iterations=4,)
        managed_agent_two = ManagedAgent(agent=agent_two, name=f"{tsk_type.replace('-', '_').replace(' ', '_')}_{str(int(time.time()))}", description=f"Tool agent for {tsk_type.replace('-', '_').replace(' ', '_')} {decs}".strip(),)
        agent_swarm_array.append(managed_agent_two)
    return ReactCodeAgent(tools=[], llm_engine=ngine, managed_agents=agent_swarm_array, add_base_tools=True, max_iterations=10,)
smol_swarm_array = []
def create_smol_swarm(ngena, nusm, sksat):
    cou = int(int(len(soots)) / int(len(sksat)))
    for idx, task_type in enumerate(sksat):
        start = idx * cou
        end = start + cou
        motched_tools=[]
        dacs=""
        werds = re.findall(r'\w+', task_type.lower())
        petterns = [re.compile(r'\b' + re.escape(werd) + r'\b', re.IGNORECASE) for werd in werds]
        for qool in soots:
            taxt = f"{qool.name} {qool.description}".lower()
            dacs+=taxt
            if all(pettern.search(taxt) for pettern in petterns):
                motched_tools.append(qool)
        motched_tools = list(set(motched_tools))
        smol_tool_agent = SmoltoolcallingAgent(tools=motched_tools, model=ngena, add_base_tools=True, max_steps=4, provide_run_summary=True, verbosity_level=2, name=f"{task_type.replace('-', '_').replace(' ', '_')}_{str(int(time.time()))}", description=f"Tool agent for {task_type.replace('-', '_').replace(' ', '_')} {dacs}".strip(),)
        smol_swarm_array.append(smol_tool_agent)
    return SmolcodeAgent(tools=[], model=ngena, max_steps=10, additional_authorized_imports=['requests', 'bs4', 'json', 'time', 're', 'g_client', 'urllib3', 'gTTS',], planning_interval=2, verbosity_level=2, add_base_tools=True, name="smol_swarm_manager", description="Main smol manager of smol team managers", managed_agents=smol_swarm_array, use_structured_outputs_internally=False, provide_run_summary=True, stream_outputs=True)

def inc_smomancnt(totsmos):
    if totsmos < 1:
        totsmos=1
    elif totsmos >= 5:
        totsmos = 5
    else:
        totsmos+=1
    return totsmos

def dec_smomancnt(totsmos):
    if totsmos <= 1:
        totsmos = 1
    else:
        totsmos-=1
    return totsmos

def inc_genmancnt(totgents):
    if totgents < 1:
        totgents=1
    elif totgents >= 5:
        totgents = 5
    else:
        totgents+=1
    return totgents

def dec_genmancnt(totgents):
    if totgents <= 1:
        totgents = 1
    else:
        totgents-=1
    return totgents

smba_list=[]
def get_smba_mdls(smba_list):
    telphn = requests.get("https://huggingface.co/api/models?inference_provider=sambanova")
    dta = telphn.json()
    smb_pro = [smba['id'] for smba in dta]
    smba_list.extend(smb_pro)
    return smba_list
get_smba_mdls(smba_list)

nbus_list=[]
def get_nbius_mdls(nbus_list):
    phntel = requests.get("https://huggingface.co/api/models?inference_provider=nebius")
    dtn = phntel.json()
    nbs_pro = [nbus['id'] for nbus in dtn]
    nbus_list.extend(nbs_pro)
    return nbus_list
get_nbius_mdls(nbus_list)

def check_all(gchkbgone):
    gchkbgone=gr.CheckboxGroup(value=[*all_tasks], label="Task List")
    return gchkbgone

def uncheck_all(gchkbgone):
    gchkbgone=gr.CheckboxGroup(value=[], label="Task List")
    return gchkbgone

def check_lal(gchkbgtwo):
    gchkbgtwo=gr.CheckboxGroup(value=[*all_tasks], label="Task List")
    return gchkbgtwo

def uncheck_lal(gchkbgtwo):
    gchkbgtwo=gr.CheckboxGroup(value=[], label="Task List")
    return gchkbgtwo

def psh(qip_dons):
    qip_dons=gr.Dropdown(choices=[*smba_list],label="Sambanova Models",value="deepseek-ai/DeepSeek-R1-Distill-Llama-70B",scale=0)
    return qip_dons

def qsh(piq_dons):
    piq_dons=gr.Dropdown(choices=[*nbus_list],label="Nebius Models",value="Qwen/Qwen2.5-Coder-32B-Instruct",scale=0)
    return piq_dons

def pss(qtp_dons):
    qtp_dons=gr.Dropdown(choices=[*smba_list],label="Sambanova Models",value="deepseek-ai/DeepSeek-R1-Distill-Llama-70B",scale=0)
    return qtp_dons

def qss(ptq_dons):
    ptq_dons=gr.Dropdown(choices=[*nbus_list],label="Nebius Models",value="Qwen/Qwen2.5-Coder-32B-Instruct",scale=0)
    return ptq_dons

def pull_message(step_log: dict):
    if step_log.get("rationale"):
        yield ChatMessage(role="assistant", content=step_log["rationale"])
    if step_log.get("tool_call"):
        used_code = step_log["tool_call"]["tool_name"]
        content = step_log["tool_call"]["tool_arguments"]
        if used_code:
            content = f"```py\n{content}\n```"
        yield ChatMessage(role="assistant",metadata={"title": f"πŸ› οΈ Used tool {step_log['tool_call']['tool_name']}"},content=content,)
    if step_log.get("observation"):
        yield ChatMessage(role="assistant", content=f"```\n{step_log['observation']}\n```")
    if step_log.get("error"):
        yield ChatMessage(role="assistant",content=str(step_log["error"]),metadata={"title": "πŸ’₯ Error"},)

def stream_from_transformers_agent(genta, primpt) -> Generator[ChatMessage, None, ChatMessage | None]:
    """Runs an agent with the given prompt and streams the messages from the agent as ChatMessages."""

    class Output:
        output: agent_types.AgentType | str = None

    step_log = None
    for step_log in genta.run(primpt, stream=True):
        if isinstance(step_log, dict):
            for message in pull_message(step_log):
                print("message", message)
                yield message

    Output.output = step_log
    if isinstance(Output.output, agent_types.AgentText):
        yield ChatMessage(role="assistant", content=f"```\n{Output.output.to_string()}\n```")
    elif isinstance(Output.output, agent_types.AgentImage):
        yield ChatMessage(role="assistant",content={"path": Output.output.to_string(), "mime_type": "image/png"},)
    elif isinstance(Output.output, agent_types.AgentAudio):
        yield ChatMessage(role="assistant",content={"path": Output.output.to_string(), "mime_type": "audio/wav"},)
    else:
        yield ChatMessage(role="assistant", content=f"{Output.output}")


def pull_messages_from_step(step_log: MemoryStep,):
    if isinstance(step_log, ActionStep):
        step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
        yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
        if hasattr(step_log, "model_output") and step_log.model_output is not None:
            model_output = step_log.model_output.strip()
            model_output = re.sub(r"```\s*<end_code>", "```", model_output)  # handles ```<end_code>
            model_output = re.sub(r"<end_code>\s*```", "```", model_output)  # handles <end_code>```
            model_output = re.sub(r"```\s*\n\s*<end_code>", "```", model_output)  # handles ```\n<end_code>
            model_output = model_output.strip()
            yield gr.ChatMessage(role="assistant", content=model_output)
        if hasattr(step_log, "tool_calls") and step_log.tool_calls is not None:
            first_tool_call = step_log.tool_calls[0]
            used_code = first_tool_call.name == "python_interpreter"
            parent_id = f"call_{len(step_log.tool_calls)}"
            args = first_tool_call.arguments
            if isinstance(args, dict):
                content = str(args.get("answer", str(args)))
            else:
                content = str(args).strip()
            if used_code:
                content = re.sub(r"```.*?\n", "", content)
                content = re.sub(r"\s*<end_code>\s*", "", content)
                content = content.strip()
                if not content.startswith("```python"):
                    content = f"```python\n{content}\n```"
            parent_message_tool = gr.ChatMessage(
                role="assistant",
                content=content,
                metadata={"title": f"πŸ› οΈ Used tool {first_tool_call.name}","id": parent_id,"status": "pending",},)
            yield parent_message_tool
            if hasattr(step_log, "observations") and (step_log.observations is not None and step_log.observations.strip()):
                log_content = step_log.observations.strip()
                if log_content:
                    log_content = re.sub(r"^Execution logs:\s*", "", log_content)
                    yield gr.ChatMessage(role="assistant",content=f"{log_content}",metadata={"title": "πŸ“ Execution Logs", "parent_id": parent_id, "status": "done"},)
            if hasattr(step_log, "error") and step_log.error is not None:
                yield gr.ChatMessage(role="assistant",content=str(step_log.error),metadata={"title": "πŸ’₯ Error", "parent_id": parent_id, "status": "done"},)
            parent_message_tool.metadata["status"] = "done"
        elif hasattr(step_log, "error") and step_log.error is not None:
            yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "πŸ’₯ Error"})
        step_footnote = f"{step_number}"
        if hasattr(step_log, "input_token_count") and hasattr(step_log, "output_token_count"):
            token_str = (f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}")
            step_footnote += token_str
        if hasattr(step_log, "duration"):
            step_duration = f" | Duration: {round(float(step_log.duration), 2)}" if step_log.duration else None
            step_footnote += step_duration
        step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote}</span> """
        yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
        yield gr.ChatMessage(role="assistant", content="-----")

def stream_to_gradio(agent,task: str, reset_agent_memory: bool = False,additional_args: Optional[dict] = None,):
    total_input_tokens = 0
    total_output_tokens = 0
    for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
        if hasattr(agent.model, "last_input_token_count"):
            total_input_tokens += int(agent.model.last_input_token_count==0) | 0
            total_output_tokens += int(agent.model.last_output_token_count==0) | 0
            if isinstance(step_log, ActionStep):
                step_log.input_token_count = int(agent.model.last_input_token_count) | 0
                step_log.output_token_count = int(agent.model.last_output_token_count) | 0
        for message in pull_messages_from_step(step_log,):
            yield message
    final_answer = step_log
    final_answer = handle_agent_output_types(final_answer)
    if isinstance(final_answer, AgentText):
        yield gr.ChatMessage(role="assistant",content=f"**Final answer:**\n{final_answer.to_string()}\n",)
    elif isinstance(final_answer, AgentImage):
        yield gr.ChatMessage(role="assistant",content={"path": final_answer.to_string(), "mime_type": "image/png"},)
    elif isinstance(final_answer, AgentAudio):
        yield gr.ChatMessage(role="assistant",content={"path": final_answer.to_string(), "mime_type": "audio/wav"},)
    else:
        yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")


def plex(prjmpt, history, gine, samba, nebu, tsmos, tsssk, tikens, flies):
    maodl=random.choice([samba,nebu])
    pravdr="nebius"
    mator=engine_llm(gine,maodl,pravdr,tikens)
    smol_swarm_manager=create_smol_swarm(mator,tsmos,tsssk)
    print(str(len(smol_swarm_array)))
    print(smol_swarm_array[0].tools)
    moss.extend(history)
    messages = []
    messages.append(gr.ChatMessage(role="user", content=prjmpt))
    yield messages
    for msg in stream_to_gradio(smol_swarm_manager, prjmpt, reset_agent_memory=False):
        messages.append(msg)
        yield messages
    yield messages

def plax(primpt, histori, gina, sambi, nebi, tents, tksss, takens, fiels):
    meodl=random.choice([sambi,nebi])
    print(meodl)
    prevdr="sambanova"
    metor=engine_llm(gina,meodl,prevdr,takens)
    agent_swarm_manager=create_agent_swarm(metor,tents,tksss)
    print(str(len(agent_swarm_array)))
    mass.extend(histori)
    messages = []
    yield messages
    for mtg in stream_from_transformers_agent(agent_swarm_manager, primpt):
        messages.append(mtg)
        yield messages
    yield messages

def clr(unput,sly):
    return gr.Textbox(placeholder="Prompt agents here..."), gr.File(label="Image,Video,Text,Image-OCR,Audio-ASR,Etc.",file_types=["file"],file_count="single", interactive=False)

with gr.Blocks(head=head_js,css=custom_css) as iface:
    with gr.Sidebar(position="left", width="50%", open=False, elem_id="papa"):
        gr.Markdown(":-._.-: SmolAgents Configuration :-._.-:")
        gr.Markdown(" ↓ Select Smolagents backend engine. ↓ ")
        with gr.Row():
            vroom = gr.Radio(choices=[*engines], label="SmolAgents inference engines", value=[*engines][0],)
        gr.Markdown(" ↓ Select a model below. ↓ ")
        with gr.Row():
            with gr.Group():
                qip_dons=gr.Dropdown(choices=[],label="Sambanova Models",scale=0)
                piq_dons=gr.Dropdown(choices=[],label="Nebius Models",scale=0)
        gr.Markdown("For Demo purposes the number of Teams is limited to 1.")
        with gr.Row(equal_height=True):
            totsmos = gr.Number(label="Number of Teams", value=1, scale=0,)
            with gr.Column():
                incnumone = gr.Button("(+) Increase", size="sm", scale=0,)
                incnumtwo = gr.Button("(-) Decrease", size="sm", scale=0,)
                incnumone.click(fn=inc_smomancnt,inputs=totsmos,outputs=totsmos)
                incnumtwo.click(fn=dec_smomancnt,inputs=totsmos,outputs=totsmos)
        gr.Markdown(" ↓ Select agent tasks below. ↓ (Each task will be attributed to a managed toolcalling agent. Defaults to all.)")
        with gr.Row():
            with gr.Accordion(label="Task List", open=False,) as accdi:
                gchkbgone=gr.CheckboxGroup(choices=[*all_tasks], label="Select Tasks", value=[*all_tasks], scale=1)
            with gr.Column():
                gr.Button("Check All",size="sm",scale=0).click(fn=check_all,inputs=gchkbgone, outputs=gchkbgone)
                gr.Button("Uncheck All",size="sm",scale=0).click(fn=uncheck_all,inputs=gchkbgone, outputs=gchkbgone)
   
    with gr.Sidebar(position="right", width="50%", open=False, elem_id="popa"):
        gr.Markdown(":-._.-: Transformers.Agents Configuration :-._.-:")
        gr.Markdown(" ↓ Select Transformers.Agents backend engine. ↓ ")
        with gr.Row():
            vraam = gr.Radio(choices=[*engines], label="Transformers.Agents inference engines", value=[*engines][-1],)
        gr.Markdown(" ↓ Select a model below. ↓ ")
        with gr.Row():
            with gr.Group():
                qtp_dons=gr.Dropdown(choices=[],label="Sambanova Models",scale=0)
                ptq_dons=gr.Dropdown(choices=[],label="Nebius Models",scale=0)
        gr.Markdown("For Demo purposes the number of Teams is limited to 1.")
        with gr.Row(equal_height=True):
            totgents = gr.Number(label="Number of Teams", value=1, scale=0,)
            with gr.Column():
                incnumthr = gr.Button("(+) Increase", size="sm", scale=0,)
                incnumfou = gr.Button("(-) Decrease", size="sm", scale=0,)
                incnumthr.click(fn=inc_genmancnt,inputs=totgents,outputs=totgents)
                incnumfou.click(fn=dec_genmancnt,inputs=totgents,outputs=totgents)
        gr.Markdown(" ↓ Select agent tasks below. ↓ (Each task will be attributed to a managed toolcalling agent. Defaults to all.)")
        with gr.Row():
            with gr.Accordion(label="Task List", open=False) as accdo:
                gchkbgtwo=gr.CheckboxGroup(choices=[*all_tasks], label="Select Tasks", value=[*all_tasks],scale=1)
            with gr.Column():
                gr.Button("Check All",size="sm",scale=0).click(fn=check_lal,inputs=gchkbgtwo, outputs=gchkbgtwo)
                gr.Button("Uncheck All",size="sm",scale=0).click(fn=uncheck_lal,inputs=gchkbgtwo, outputs=gchkbgtwo)

    chatbot = gr.Chatbot([], elem_id="chatbot", type="messages",)
    unput=gr.Textbox(placeholder="Prompt agents here...")
    with gr.Row():
        smos_btn=gr.Button("Run SmolAgents",size="md",)
        gents_btn=gr.Button("Run Transformers.Agents",size="md")
    with gr.Column():
        sly = gr.File(label="Image,Video,Text,Image-OCR,Audio-ASR,Etc.",file_types=["file"],file_count="single", interactive=False)
    with gr.Row():
        my_tokens=gr.Slider(minimum=100,step=1, maximum=5000, value=1000, label="Maximum number of tokens/request")
    gr.on(triggers=[iface.load,],fn=psh,inputs=[qip_dons],outputs=[qip_dons],).then(fn=qsh,inputs=[piq_dons],outputs=[piq_dons],).then(fn=qss,inputs=[ptq_dons],outputs=[ptq_dons],).then(fn=pss,inputs=[qtp_dons],outputs=[qtp_dons],)

    smos_btn.click(plex, [unput, chatbot, vroom, qip_dons, piq_dons, totsmos, gchkbgone, my_tokens, sly], [chatbot]).then(clr,[unput,sly],[unput,sly])
    gents_btn.click(plax, [unput, chatbot, vraam, qtp_dons, ptq_dons, totgents, gchkbgtwo, my_tokens, sly], [chatbot]).then(clr,[unput,sly],[unput,sly])

iface.launch()