sdeepanraj commited on
Commit
f9cde6b
·
verified ·
1 Parent(s): aa03cce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -5,6 +5,10 @@ import inspect
5
  import pandas as pd
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, InferenceClientModel, WikipediaSearchTool, VisitWebpageTool, TransformersModel
7
  from huggingface_hub import login
 
 
 
 
8
  import os
9
 
10
 
@@ -18,35 +22,33 @@ token = os.getenv('HF_API_TOKEN')
18
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
19
  class BasicAgent:
20
  def __init__(self):
 
21
  print("BasicAgent initialized.")
22
  # Ensure the token is not None
23
  if token is None:
24
  print("Hugging Face API token not found in environment variables.")
25
- #login()
26
- #model = HfApiModel(
27
- # max_tokens=2096,
28
- # temperature=0.5,
29
- # model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
30
- # custom_role_conversions=None,
31
- #)
32
-
33
- #model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-1.5B-Instruct",
34
- # max_tokens=2096,
35
- # token=token,
36
- # custom_role_conversions=None)
37
-
38
- # Initialize the model with TransformersModel
39
- model = TransformersModel(
40
- model_id="gpt2",
41
- max_new_tokens=4096,
 
 
42
  device_map="auto"
43
  )
44
- self.agent = CodeAgent(
45
- #model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-7B-Instruct",token=token),
46
- model = model,
47
- tools = [DuckDuckGoSearchTool(),WikipediaSearchTool(),VisitWebpageTool()],
48
- #additional_authorized_imports = ["pandas", "numpy"]
49
- )
50
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
51
  Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
52
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
@@ -54,6 +56,13 @@ class BasicAgent:
54
  If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
55
  If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
56
  """
 
 
 
 
 
 
 
57
  self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + SYSTEM_PROMPT
58
 
59
  def __call__(self, question: str) -> str:
 
5
  import pandas as pd
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, InferenceClientModel, WikipediaSearchTool, VisitWebpageTool, TransformersModel
7
  from huggingface_hub import login
8
+ from transformers import (
9
+ AutoTokenizer,
10
+ AutoModelForCausalLM,
11
+ )
12
  import os
13
 
14
 
 
22
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
23
  class BasicAgent:
24
  def __init__(self):
25
+ os.environ["HF_HOME"] = "/data/.huggingface"
26
  print("BasicAgent initialized.")
27
  # Ensure the token is not None
28
  if token is None:
29
  print("Hugging Face API token not found in environment variables.")
30
+
31
+ model_name = "gpt2" # or "gpt2-medium", "gpt2-large"
32
+
33
+ # Load tokenizer and inject a minimal chat_template
34
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
35
+ if not getattr(tokenizer, "chat_template", None):
36
+ tokenizer.chat_template = (
37
+ "<|im_start|>system\n"
38
+ "{system}\n"
39
+ "<|im_end|>\n"
40
+ "<|im_start|>user\n"
41
+ "{user}\n"
42
+ "<|im_end|>\n"
43
+ "<|im_start|>assistant\n"
44
+ )
45
+
46
+ # Load the GPT-2 model (FP16 if you like, but default is fine)
47
+ model = AutoModelForCausalLM.from_pretrained(
48
+ model_name,
49
  device_map="auto"
50
  )
51
+
 
 
 
 
 
52
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
53
  Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
54
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
 
56
  If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
57
  If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
58
  """
59
+
60
+ self.agent = CodeAgent(
61
+ model = model,
62
+ tools = [DuckDuckGoSearchTool(),WikipediaSearchTool(),VisitWebpageTool()],
63
+ #additional_authorized_imports = ["pandas", "numpy"]
64
+ )
65
+
66
  self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + SYSTEM_PROMPT
67
 
68
  def __call__(self, question: str) -> str: