parent
3297f7fc61
commit
68605261f0
@ -0,0 +1,25 @@
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Transcript Generator",
|
||||
system_prompt="Generate a transcript for a youtube video on what swarms are!",
|
||||
agent_description=(
|
||||
"Generate a transcript for a youtube video on what swarms" " are!"
|
||||
),
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
interactive=False,
|
||||
state_save_file_type="json",
|
||||
saved_state_path="transcript_generator.json",
|
||||
agent_ops_on=True,
|
||||
)
|
||||
|
||||
# Run the Agent on a task
|
||||
agent.run("Generate a transcript for a youtube video on what swarms are!")
|
@ -0,0 +1,82 @@
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
|
||||
def calculate_profit(revenue: float, expenses: float):
|
||||
"""
|
||||
Calculates the profit by subtracting expenses from revenue.
|
||||
|
||||
Args:
|
||||
revenue (float): The total revenue.
|
||||
expenses (float): The total expenses.
|
||||
|
||||
Returns:
|
||||
float: The calculated profit.
|
||||
"""
|
||||
return revenue - expenses
|
||||
|
||||
|
||||
def generate_report(company_name: str, profit: float):
|
||||
"""
|
||||
Generates a report for a company's profit.
|
||||
|
||||
Args:
|
||||
company_name (str): The name of the company.
|
||||
profit (float): The calculated profit.
|
||||
|
||||
Returns:
|
||||
str: The report for the company's profit.
|
||||
"""
|
||||
return f"The profit for {company_name} is ${profit}."
|
||||
|
||||
|
||||
def account_agent(task: str = None):
|
||||
"""
|
||||
delegate a task to an agent!
|
||||
|
||||
Task: str (What task to give to an agent)
|
||||
|
||||
"""
|
||||
agent = Agent(
|
||||
agent_name="Finance Agent",
|
||||
system_prompt="You're the Finance agent, your purpose is to generate a profit report for a company!",
|
||||
agent_description="Generate a profit report for a company!",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dynamic_temperature_enabled=True,
|
||||
dashboard=False,
|
||||
verbose=True,
|
||||
# interactive=True, # Set to False to disable interactive mode
|
||||
# stopping_token="<DONE>",
|
||||
# saved_state_path="accounting_agent.json",
|
||||
# tools=[calculate_profit, generate_report],
|
||||
# docs_folder="docs",
|
||||
# pdf_path="docs/accounting_agent.pdf",
|
||||
)
|
||||
|
||||
out = agent.run(task)
|
||||
return out
|
||||
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Accounting Assistant",
|
||||
system_prompt="You're the accounting agent, your purpose is to generate a profit report for a company!",
|
||||
agent_description="Generate a profit report for a company!",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dynamic_temperature_enabled=True,
|
||||
dashboard=False,
|
||||
verbose=True,
|
||||
# interactive=True, # Set to False to disable interactive mode
|
||||
# stopping_token="<DONE>",
|
||||
# saved_state_path="accounting_agent.json",
|
||||
tools=[account_agent],
|
||||
# docs_folder="docs",
|
||||
# pdf_path="docs/accounting_agent.pdf",
|
||||
)
|
||||
|
||||
agent.run(
|
||||
"Delegate a task to the accounting agent: what are the best ways to read cashflow statements"
|
||||
)
|
@ -1,182 +0,0 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from functools import wraps
|
||||
from typing import Union
|
||||
|
||||
import torch
|
||||
from termcolor import colored
|
||||
from transformers import (
|
||||
AutoModelForSpeechSeq2Seq,
|
||||
AutoProcessor,
|
||||
pipeline,
|
||||
)
|
||||
|
||||
|
||||
def async_retry(max_retries=3, exceptions=(Exception,), delay=1):
|
||||
"""
|
||||
A decorator for adding retry logic to async functions.
|
||||
:param max_retries: Maximum number of retries before giving up.
|
||||
:param exceptions: A tuple of exceptions to catch and retry on.
|
||||
:param delay: Delay between retries.
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
async def wrapper(*args, **kwargs):
|
||||
retries = max_retries
|
||||
while retries:
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except exceptions as e:
|
||||
retries -= 1
|
||||
if retries <= 0:
|
||||
raise
|
||||
print(
|
||||
f"Retry after exception: {e}, Attempts"
|
||||
f" remaining: {retries}"
|
||||
)
|
||||
await asyncio.sleep(delay)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class DistilWhisperModel:
|
||||
"""
|
||||
This class encapsulates the Distil-Whisper model for English speech recognition.
|
||||
It allows for both synchronous and asynchronous transcription of short and long-form audio.
|
||||
|
||||
Args:
|
||||
model_id: The model ID to use. Defaults to "distil-whisper/distil-large-v2".
|
||||
|
||||
|
||||
Attributes:
|
||||
device: The device to use for inference.
|
||||
torch_dtype: The torch data type to use for inference.
|
||||
model_id: The model ID to use.
|
||||
model: The model instance.
|
||||
processor: The processor instance.
|
||||
|
||||
Usage:
|
||||
model_wrapper = DistilWhisperModel()
|
||||
transcription = model_wrapper('path/to/audio.mp3')
|
||||
|
||||
# For async usage
|
||||
transcription = asyncio.run(model_wrapper.async_transcribe('path/to/audio.mp3'))
|
||||
"""
|
||||
|
||||
def __init__(self, model_id="distil-whisper/distil-large-v2"):
|
||||
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
||||
self.torch_dtype = (
|
||||
torch.float16 if torch.cuda.is_available() else torch.float32
|
||||
)
|
||||
self.model_id = model_id
|
||||
self.model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
||||
model_id,
|
||||
torch_dtype=self.torch_dtype,
|
||||
low_cpu_mem_usage=True,
|
||||
use_safetensors=True,
|
||||
).to(self.device)
|
||||
self.processor = AutoProcessor.from_pretrained(model_id)
|
||||
|
||||
def __call__(self, inputs: Union[str, dict]):
|
||||
return self.transcribe(inputs)
|
||||
|
||||
def transcribe(self, inputs: Union[str, dict]):
|
||||
"""
|
||||
Synchronously transcribe the given audio input using the Distil-Whisper model.
|
||||
:param inputs: A string representing the file path or a dict with audio data.
|
||||
:return: The transcribed text.
|
||||
"""
|
||||
pipe = pipeline(
|
||||
"automatic-speech-recognition",
|
||||
model=self.model,
|
||||
tokenizer=self.processor.tokenizer,
|
||||
feature_extractor=self.processor.feature_extractor,
|
||||
max_new_tokens=128,
|
||||
torch_dtype=self.torch_dtype,
|
||||
device=self.device,
|
||||
)
|
||||
|
||||
return pipe(inputs)["text"]
|
||||
|
||||
@async_retry()
|
||||
async def async_transcribe(self, inputs: Union[str, dict]):
|
||||
"""
|
||||
Asynchronously transcribe the given audio input using the Distil-Whisper model.
|
||||
:param inputs: A string representing the file path or a dict with audio data.
|
||||
:return: The transcribed text.
|
||||
"""
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(None, self.transcribe, inputs)
|
||||
|
||||
def real_time_transcribe(self, audio_file_path, chunk_duration=5):
|
||||
"""
|
||||
Simulates real-time transcription of an audio file, processing and printing results
|
||||
in chunks with colored output for readability.
|
||||
|
||||
:param audio_file_path: Path to the audio file to be transcribed.
|
||||
:param chunk_duration: Duration in seconds of each audio chunk to be processed.
|
||||
"""
|
||||
if not os.path.isfile(audio_file_path):
|
||||
print(colored("The audio file was not found.", "red"))
|
||||
return
|
||||
|
||||
# Assuming `chunk_duration` is in seconds and `processor` can handle chunk-wise processing
|
||||
try:
|
||||
with torch.no_grad():
|
||||
# Load the whole audio file, but process and transcribe it in chunks
|
||||
audio_input = self.processor.audio_file_to_array(
|
||||
audio_file_path
|
||||
)
|
||||
sample_rate = audio_input.sampling_rate
|
||||
len(audio_input.array) / sample_rate
|
||||
chunks = [
|
||||
audio_input.array[i : i + sample_rate * chunk_duration]
|
||||
for i in range(
|
||||
0,
|
||||
len(audio_input.array),
|
||||
sample_rate * chunk_duration,
|
||||
)
|
||||
]
|
||||
|
||||
print(
|
||||
colored("Starting real-time transcription...", "green")
|
||||
)
|
||||
|
||||
for i, chunk in enumerate(chunks):
|
||||
# Process the current chunk
|
||||
processed_inputs = self.processor(
|
||||
chunk,
|
||||
sampling_rate=sample_rate,
|
||||
return_tensors="pt",
|
||||
padding=True,
|
||||
)
|
||||
processed_inputs = processed_inputs.input_values.to(
|
||||
self.device
|
||||
)
|
||||
|
||||
# Generate transcription for the chunk
|
||||
logits = self.model.generate(processed_inputs)
|
||||
transcription = self.processor.batch_decode(
|
||||
logits, skip_special_tokens=True
|
||||
)[0]
|
||||
|
||||
# Print the chunk's transcription
|
||||
print(
|
||||
colored(f"Chunk {i+1}/{len(chunks)}: ", "yellow")
|
||||
+ transcription
|
||||
)
|
||||
|
||||
# Wait for the chunk's duration to simulate real-time processing
|
||||
time.sleep(chunk_duration)
|
||||
|
||||
except Exception as e:
|
||||
print(
|
||||
colored(
|
||||
f"An error occurred during transcription: {e}",
|
||||
"red",
|
||||
)
|
||||
)
|
@ -0,0 +1,48 @@
|
||||
from typing import List
|
||||
import json
|
||||
import loguru
|
||||
from swarms.utils.parse_code import extract_code_from_markdown
|
||||
|
||||
|
||||
def parse_and_execute_json(
|
||||
functions: List[callable] = None,
|
||||
json_string: str = None,
|
||||
parse_md: bool = False,
|
||||
):
|
||||
"""
|
||||
Parses and executes a JSON string containing function name and parameters.
|
||||
|
||||
Args:
|
||||
functions (List[callable]): A list of callable functions.
|
||||
json_string (str): The JSON string to parse and execute.
|
||||
parse_md (bool): Flag indicating whether to extract code from Markdown.
|
||||
|
||||
Returns:
|
||||
The result of executing the function with the parsed parameters, or None if an error occurs.
|
||||
|
||||
"""
|
||||
if parse_md:
|
||||
json_string = extract_code_from_markdown(json_string)
|
||||
|
||||
try:
|
||||
# Create a dictionary that maps function names to functions
|
||||
function_dict = {func.__name__: func for func in functions}
|
||||
|
||||
loguru.logger.info(f"Extracted code: {json_string}")
|
||||
data = json.loads(json_string)
|
||||
function_name = data.get("function", {}).get("name")
|
||||
parameters = data.get("function", {}).get("parameters")
|
||||
|
||||
# Check if the function name is in the function dictionary
|
||||
if function_name in function_dict:
|
||||
# Call the function with the parsed parameters
|
||||
result = function_dict[function_name](**parameters)
|
||||
return result
|
||||
else:
|
||||
loguru.logger.warning(
|
||||
f"No function named '{function_name}' found."
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
loguru.logger.error(f"Error: {e}")
|
||||
return None
|
@ -0,0 +1,28 @@
|
||||
from swarms.utils.loguru_logger import logger
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
def try_import_agentops(*args, **kwargs):
|
||||
try:
|
||||
load_dotenv()
|
||||
logger.info("Trying to import agentops")
|
||||
import agentops
|
||||
|
||||
agentops.init(os.getenv("AGENTOPS_API_KEY"), *args, **kwargs)
|
||||
|
||||
return "agentops imported successfully."
|
||||
except ImportError:
|
||||
logger.error("Could not import agentops")
|
||||
|
||||
|
||||
def end_session_agentops():
|
||||
try:
|
||||
logger.info("Trying to end session")
|
||||
import agentops
|
||||
|
||||
agentops.end_session("Success")
|
||||
return "Session ended successfully."
|
||||
except ImportError:
|
||||
logger.error("Could not import agentops")
|
||||
return "Could not end session."
|
Loading…
Reference in new issue