SrirajHunt commited on
Commit
aaedf0d
·
verified ·
1 Parent(s): 2c89473

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -5,18 +5,19 @@ from Gradio_UI import GradioUI
5
  import datetime
6
  import pytz
7
  import yaml
8
- import random
9
 
10
- # Create the Hugging Face Inference client using your HF token from environment variable
11
- client = InferenceClient(token=None) # token is read automatically from HF_TOKEN secret
12
 
13
- # Wrap client in a simple model class for smolagents
 
 
 
 
14
  class HFModelWrapper:
15
  def __init__(self, client, model_id):
16
  self.client = client
17
  self.model_id = model_id
18
 
19
- def __call__(self, prompt, max_tokens=2048, temperature=0.5):
20
  response = self.client.text_generation(
21
  model=self.model_id,
22
  inputs=prompt,
@@ -25,20 +26,24 @@ class HFModelWrapper:
25
  )
26
  return response.generated_text
27
 
28
- # Tools (same as before)
29
 
30
  @tool
31
- def get_current_time_in_timezone(timezone: str) -> str:
32
- try:
33
- tz = pytz.timezone(timezone)
34
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
35
- return f"The current local time in {timezone} is {local_time}."
36
- except Exception as e:
37
- return f"Error: invalid timezone '{timezone}'. Details: {str(e)}"
38
 
39
 
40
  @tool
41
  def calculator(operation: str, a: float, b: float) -> str:
 
 
 
 
 
 
42
  try:
43
  if operation == "add":
44
  return f"{a} + {b} = {a + b}"
@@ -51,25 +56,23 @@ def calculator(operation: str, a: float, b: float) -> str:
51
  return "Error: Division by zero is not allowed."
52
  return f"{a} / {b} = {a / b}"
53
  else:
54
- return "Error: Operation must be add, subtract, multiply, or divide."
55
  except Exception as e:
56
  return f"Calculation error: {str(e)}"
57
 
58
 
59
  @tool
60
- def greet(name: str) -> str:
61
- return f"Hello, {name}! How can I assist you today?"
62
-
63
-
64
- @tool
65
- def tell_joke() -> str:
66
- jokes = [
67
- "Why don't scientists trust atoms? Because they make up everything!",
68
- "Why did the bicycle fall over? Because it was two-tired!",
69
- "What do you call fake spaghetti? An impasta!",
70
- "Why did the scarecrow win an award? Because he was outstanding in his field!"
71
- ]
72
- return random.choice(jokes)
73
 
74
 
75
  final_answer = FinalAnswerTool()
@@ -77,12 +80,11 @@ final_answer = FinalAnswerTool()
77
  with open("prompts.yaml", "r") as f:
78
  prompt_templates = yaml.safe_load(f)
79
 
80
-
81
  model = HFModelWrapper(client=client, model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
82
 
83
  agent = CodeAgent(
84
  model=model,
85
- tools=[final_answer, get_current_time_in_timezone, calculator, greet, tell_joke],
86
  max_steps=6,
87
  verbosity_level=1,
88
  prompt_templates=prompt_templates,
 
5
  import datetime
6
  import pytz
7
  import yaml
 
8
 
 
 
9
 
10
+ # Initialize HF client (token from HF_TOKEN secret automatically)
11
+ client = InferenceClient()
12
+
13
+
14
+ # Wrap client to work as model callable
15
  class HFModelWrapper:
16
  def __init__(self, client, model_id):
17
  self.client = client
18
  self.model_id = model_id
19
 
20
+ def __call__(self, prompt, max_tokens=512, temperature=0.5):
21
  response = self.client.text_generation(
22
  model=self.model_id,
23
  inputs=prompt,
 
26
  )
27
  return response.generated_text
28
 
 
29
 
30
  @tool
31
+ def say_hi(name: str) -> str:
32
+ """Returns a greeting message.
33
+ Args:
34
+ name: The name of the person to greet.
35
+ """
36
+ return f"Hi {name}! How can I help you today?"
 
37
 
38
 
39
  @tool
40
  def calculator(operation: str, a: float, b: float) -> str:
41
+ """Performs basic arithmetic operations.
42
+ Args:
43
+ operation: One of 'add', 'subtract', 'multiply', or 'divide'.
44
+ a: First number.
45
+ b: Second number.
46
+ """
47
  try:
48
  if operation == "add":
49
  return f"{a} + {b} = {a + b}"
 
56
  return "Error: Division by zero is not allowed."
57
  return f"{a} / {b} = {a / b}"
58
  else:
59
+ return "Error: Operation must be 'add', 'subtract', 'multiply', or 'divide'."
60
  except Exception as e:
61
  return f"Calculation error: {str(e)}"
62
 
63
 
64
  @tool
65
+ def get_current_time_in_timezone(timezone: str) -> str:
66
+ """Returns the current local time in a specified timezone.
67
+ Args:
68
+ timezone: Valid timezone string, e.g. 'America/New_York'.
69
+ """
70
+ try:
71
+ tz = pytz.timezone(timezone)
72
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
73
+ return f"The current local time in {timezone} is {local_time}."
74
+ except Exception as e:
75
+ return f"Error: invalid timezone '{timezone}'. Details: {str(e)}"
 
 
76
 
77
 
78
  final_answer = FinalAnswerTool()
 
80
  with open("prompts.yaml", "r") as f:
81
  prompt_templates = yaml.safe_load(f)
82
 
 
83
  model = HFModelWrapper(client=client, model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
84
 
85
  agent = CodeAgent(
86
  model=model,
87
+ tools=[final_answer, say_hi, calculator, get_current_time_in_timezone],
88
  max_steps=6,
89
  verbosity_level=1,
90
  prompt_templates=prompt_templates,