ressay1973 commited on
Commit
dd7a51a
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -36
app.py CHANGED
@@ -1,69 +1,90 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
- import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
20
 
 
21
  @tool
22
- def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
  Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
27
  try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
39
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
-
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
-
50
- # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
-
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
63
- name=None,
64
- description=None,
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
  import datetime
 
3
  import pytz
4
  import yaml
5
+ import whisper
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
8
 
9
+ # Tool: Convert time between time zones
10
+ @tool
11
+ def convert_time(time_str: str, from_tz: str, to_tz: str) -> str:
12
+ """Convert time from one timezone to another.
13
+ Args:
14
+ time_str: Time in 'YYYY-MM-DD HH:MM:SS' format.
15
+ from_tz: Original timezone.
16
+ to_tz: Target timezone.
17
+ """
18
+ try:
19
+ from_zone = pytz.timezone(from_tz)
20
+ to_zone = pytz.timezone(to_tz)
21
+ local_time = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
22
+ local_time = from_zone.localize(local_time)
23
+ converted_time = local_time.astimezone(to_zone).strftime("%Y-%m-%d %H:%M:%S")
24
+ return f"Time in {to_tz}: {converted_time}"
25
+ except Exception as e:
26
+ return f"Error converting time: {str(e)}"
27
+
28
+
29
+ # Tool: Assign alerts to departments based on keywords
30
  @tool
31
+ def assign_alert(alert_message: str) -> str:
32
+ """Assign an alert to a department based on keywords.
 
33
  Args:
34
+ alert_message: The alert description.
 
35
  """
36
+ alert_keywords = {
37
+ "network": "Network Operations",
38
+ "server": "Infrastructure",
39
+ "database": "Database Team",
40
+ "security": "Security Operations",
41
+ "application": "Development Team"
42
+ }
43
+ for keyword, department in alert_keywords.items():
44
+ if keyword.lower() in alert_message.lower():
45
+ return f"Alert assigned to: {department}"
46
+ return "Alert could not be assigned automatically. Please check manually."
47
+
48
 
49
+ # Tool: Transcribe audio and generate a timeline
50
  @tool
51
+ def transcribe_audio(audio_path: str) -> str:
52
+ """Transcribe audio and log events with timestamps.
53
  Args:
54
+ audio_path: Path to the audio file.
55
  """
56
  try:
57
+ model = whisper.load_model("base")
58
+ result = model.transcribe(audio_path)
59
+ transcript = result["text"]
60
+ timestamped_events = [f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {line}" for line in transcript.split('.')]
61
+ return "\n".join(timestamped_events)
62
  except Exception as e:
63
+ return f"Error transcribing audio: {str(e)}"
64
 
65
 
66
  final_answer = FinalAnswerTool()
67
 
 
 
 
68
  model = HfApiModel(
69
+ max_tokens=2096,
70
+ temperature=0.5,
71
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
72
+ custom_role_conversions=None,
73
  )
74
 
 
 
 
 
75
  with open("prompts.yaml", 'r') as stream:
76
  prompt_templates = yaml.safe_load(stream)
77
+
78
  agent = CodeAgent(
79
  model=model,
80
+ tools=[final_answer, convert_time, assign_alert, transcribe_audio],
81
  max_steps=6,
82
  verbosity_level=1,
83
  grammar=None,
84
  planning_interval=None,
85
+ name="Monitoring Assistant",
86
+ description="An agent that helps with alert assignment, time conversion, monitoring tasks, and real-time transcription.",
87
  prompt_templates=prompt_templates
88
  )
89
 
90
+ GradioUI(agent).launch()