Dave67350 commited on
Commit
28a9f19
·
verified ·
1 Parent(s): bc1e1ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -69
app.py CHANGED
@@ -3,107 +3,64 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
- import os
7
  from tools.final_answer import FinalAnswerTool
8
- from Gradio_UI import GradioUI
9
-
10
-
11
- # AI Regulation Model using HfApiModel (No HF Token)
12
- class AIRegulationModel:
13
- def __init__(self, model_id="Orcawise/eu_ai_act_using_finetuned_gemma"):
14
- self.model = HfApiModel(
15
- max_tokens=500,
16
- temperature=0.5,
17
- model_id=model_id # No authentication required for public models
18
- )
19
-
20
- def get_regulation_info(self, query: str) -> str:
21
- """
22
- Generates a response based on an AI regulation query.
23
 
24
- Args:
25
- query (str): The input question related to AI regulations.
26
-
27
- Returns:
28
- str: The response from the AI regulation model, or an error message if generation fails.
29
- """
30
- response = self.model.generate(prompt=query) # Ensure correct API call
31
- if isinstance(response, dict) and "generated_text" in response:
32
- return response["generated_text"]
33
- return "No response generated. Please try again."
34
-
35
- # Initialize AI Regulation Tool
36
- ai_regulation_tool = AIRegulationModel()
37
 
 
38
  @tool
39
- def ai_regulation_query_tool(query: str) -> str:
40
- """
41
- Queries an AI model for information on AI regulation.
42
-
43
  Args:
44
- query (str): The user's question related to AI regulations.
45
-
46
- Returns:
47
- str: The response from the AI model regarding AI regulations.
48
  """
49
- return ai_regulation_tool.get_regulation_info(query)
50
 
51
  @tool
52
  def get_current_time_in_timezone(timezone: str) -> str:
53
- """
54
- Fetches the current local time in a specified timezone.
55
-
56
  Args:
57
- timezone (str): A string representing a valid timezone (e.g., 'America/New_York').
58
-
59
- Returns:
60
- str: The current time in the given timezone or an error message if the timezone is invalid.
61
  """
62
  try:
 
63
  tz = pytz.timezone(timezone)
 
64
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
65
  return f"The current local time in {timezone} is: {local_time}"
66
  except Exception as e:
67
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
68
 
69
- # Final Answer Tool
70
- final_answer = FinalAnswerTool()
71
 
72
- # Main Model (Qwen) - No HF Token required
73
- model = HfApiModel(
74
  max_tokens=2096,
75
  temperature=0.5,
76
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
77
- custom_role_conversions=None
78
  )
79
 
80
- # Import tool from Hugging Face Hub safely
81
- try:
82
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
83
- except Exception as e:
84
- print(f"Error loading image generation tool: {e}")
85
- image_generation_tool = None # Prevent crash if tool fails
86
 
87
- # Load system prompt from YAML file
88
- try:
89
- with open("prompts.yaml", "r") as stream:
90
- prompt_templates = yaml.safe_load(stream)
91
- except FileNotFoundError:
92
- print("Warning: prompts.yaml file not found. Using default templates.")
93
- prompt_templates = {}
94
 
95
- # Create Agent
 
 
 
96
  agent = CodeAgent(
97
  model=model,
98
- tools=[final_answer, ai_regulation_query_tool], # Ensure tools are in a proper list
99
  max_steps=6,
100
  verbosity_level=1,
101
  grammar=None,
102
  planning_interval=None,
103
  name=None,
104
  description=None,
105
- prompt_templates=prompt_templates
106
  )
107
 
108
- # Start Gradio UI
109
- GradioUI(agent).launch()
 
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 important to specify the return type
13
+ # Keep this format for the tool description / 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
+ model = HfApiModel(
39
  max_tokens=2096,
40
  temperature=0.5,
41
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
42
+ custom_role_conversions=None,
43
  )
44
 
 
 
 
 
 
 
45
 
46
+ # Import tool from Hub
47
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
48
 
49
+ # Load system prompt from prompt.yaml file
50
+ with open("prompts.yaml", 'r') as stream:
51
+ prompt_templates = yaml.safe_load(stream)
52
+
53
  agent = CodeAgent(
54
  model=model,
55
+ tools=[final_answer], # add your tools here (don't remove final_answer)
56
  max_steps=6,
57
  verbosity_level=1,
58
  grammar=None,
59
  planning_interval=None,
60
  name=None,
61
  description=None,
62
+ prompt_templates=prompt_templates # Pass system prompt to CodeAgent
63
  )
64
 
65
+
66
+ GradioUI(agent).launch()