wt002 commited on
Commit
6f6ec94
·
verified ·
1 Parent(s): 149648b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -24,43 +24,39 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
  load_dotenv()
25
 
26
  import os
27
- import io
28
- import contextlib
29
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
30
- from smolagents import Tool
31
 
32
- class ZephyrToolCallingAgentTool(Tool):
33
- name = "zephyr_tool_agent"
34
- description = "Uses Zephyr-7B to answer questions using code or reasoning"
35
-
36
- inputs = {
37
- "question": {
38
- "type": "string",
39
- "description": "The user's question involving reasoning or code execution."
40
- }
41
- }
42
- output_type = "string"
43
 
44
  def __init__(self):
45
- self.model_id = "HuggingFaceH4/zephyr-7b-beta"
46
- token = os.getenv("HF_TOKEN")
47
 
48
  self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, token=token)
49
  self.model = AutoModelForCausalLM.from_pretrained(
50
- self.model_id,
51
- device_map="auto",
52
- torch_dtype="auto",
53
- token=token
54
  )
 
55
  self.pipeline = pipeline(
56
  "text-generation",
57
  model=self.model,
58
  tokenizer=self.tokenizer,
59
- max_new_tokens=128, # 128 or 512
60
  temperature=0.2
61
  )
62
 
63
- def forward(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
64
  prompt = f"""You are a helpful assistant. Use code to solve questions that involve calculations.
65
  If code is needed, return a block like <tool>code</tool>. End your answer with <final>answer</final>.
66
 
@@ -69,9 +65,11 @@ Answer:"""
69
 
70
  result = self.pipeline(prompt)[0]["generated_text"]
71
 
 
72
  if "<tool>" in result and "</tool>" in result:
73
  code = result.split("<tool>")[1].split("</tool>")[0].strip()
74
- return self._run_code(code)
 
75
 
76
  elif "<final>" in result and "</final>" in result:
77
  final = result.split("<final>")[1].split("</final>")[0].strip()
@@ -79,15 +77,6 @@ Answer:"""
79
 
80
  return "Could not determine how to respond. No <tool> or <final> block detected."
81
 
82
- def _run_code(self, code: str) -> str:
83
- buffer = io.StringIO()
84
- try:
85
- with contextlib.redirect_stdout(buffer):
86
- exec(code, {})
87
- return f"FINAL ANSWER (code output): {buffer.getvalue().strip()}"
88
- except Exception as e:
89
- return f"Error during code execution: {e}"
90
-
91
 
92
  #from smolagents import Tool
93
  #from langchain_community.document_loaders import WikipediaLoader
@@ -262,7 +251,7 @@ class BasicAgent:
262
  video_transcription_tool = VideoTranscriptionTool()
263
 
264
  # ✅ New Mistral-based Tool
265
- zephyr_tool = ZephyrToolCallingAgentTool()
266
 
267
  system_prompt = f"""
268
  You are my general AI assistant. Your task is to answer the question I asked.
@@ -281,7 +270,7 @@ If the answer is a comma-separated list, apply the above rules for each element
281
  keywords_extract_tool, speech_to_text_tool,
282
  visit_webpage_tool, final_answer_tool,
283
  parse_excel_to_json, video_transcription_tool,
284
- zephyr_tool # 🔧 Add here
285
  ],
286
  add_base_tools=True
287
  )
 
24
  load_dotenv()
25
 
26
  import os
 
 
27
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
28
 
29
+ class CodeLlamaToolCallingAgentTool:
30
+ name = "code_llama_tool_agent"
31
+ description = "Uses Code Llama to answer questions using code or reasoning"
 
 
 
 
 
 
 
 
32
 
33
  def __init__(self):
34
+ self.model_id = "meta/code-llama-7b-instruct"
35
+ token = os.getenv("HF_TOKEN") # Optional unless private
36
 
37
  self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, token=token)
38
  self.model = AutoModelForCausalLM.from_pretrained(
39
+ self.model_id, device_map="auto", torch_dtype="auto", token=token
 
 
 
40
  )
41
+
42
  self.pipeline = pipeline(
43
  "text-generation",
44
  model=self.model,
45
  tokenizer=self.tokenizer,
46
+ max_new_tokens=512,
47
  temperature=0.2
48
  )
49
 
50
+ def _run_code(self, code: str) -> str:
51
+ buffer = io.StringIO()
52
+ try:
53
+ with contextlib.redirect_stdout(buffer):
54
+ exec(code, {})
55
+ return buffer.getvalue().strip()
56
+ except Exception as e:
57
+ return f"Error during code execution: {e}"
58
+
59
+ def run(self, question: str) -> str:
60
  prompt = f"""You are a helpful assistant. Use code to solve questions that involve calculations.
61
  If code is needed, return a block like <tool>code</tool>. End your answer with <final>answer</final>.
62
 
 
65
 
66
  result = self.pipeline(prompt)[0]["generated_text"]
67
 
68
+ # Process result
69
  if "<tool>" in result and "</tool>" in result:
70
  code = result.split("<tool>")[1].split("</tool>")[0].strip()
71
+ output = self._run_code(code)
72
+ return f"FINAL ANSWER (code output): {output}"
73
 
74
  elif "<final>" in result and "</final>" in result:
75
  final = result.split("<final>")[1].split("</final>")[0].strip()
 
77
 
78
  return "Could not determine how to respond. No <tool> or <final> block detected."
79
 
 
 
 
 
 
 
 
 
 
80
 
81
  #from smolagents import Tool
82
  #from langchain_community.document_loaders import WikipediaLoader
 
251
  video_transcription_tool = VideoTranscriptionTool()
252
 
253
  # ✅ New Mistral-based Tool
254
+ my_tool = CodeLlamaToolCallingAgentTool()
255
 
256
  system_prompt = f"""
257
  You are my general AI assistant. Your task is to answer the question I asked.
 
270
  keywords_extract_tool, speech_to_text_tool,
271
  visit_webpage_tool, final_answer_tool,
272
  parse_excel_to_json, video_transcription_tool,
273
+ my_tool # 🔧 Add here
274
  ],
275
  add_base_tools=True
276
  )