wt002 commited on
Commit
11e780b
·
verified ·
1 Parent(s): b411d35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -27
app.py CHANGED
@@ -23,53 +23,67 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
23
  #Load environment variables
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.1
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
-
 
 
 
 
 
 
63
  Question: {question}
64
- Answer:"""
 
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,6 +91,16 @@ Answer:"""
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
@@ -250,8 +274,8 @@ class BasicAgent:
250
  final_answer_tool = FinalAnswerTool()
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.
 
23
  #Load environment variables
24
  load_dotenv()
25
 
26
+ import io
27
+ import contextlib
28
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
29
+ from smolagents import Tool, CodeAgent, DuckDuckGoSearchTool, HfApiModel, FinalAnswerTool
30
+
31
+ class CodeLlamaTool(Tool):
32
+ name = "code_llama_tool"
33
+ description = "Uses Code Llama 7B Instruct to answer questions with code or reasoning"
34
 
35
+ inputs = {
36
+ "question": {
37
+ "type": "string",
38
+ "description": "The user's question requiring reasoning or code execution."
39
+ }
40
+ }
41
+ output_type = "string"
42
 
43
  def __init__(self):
44
+ self.model_id = "codellama/CodeLlama-7b-Instruct-hf"
45
+ token = os.getenv("HF_TOKEN")
46
 
47
  self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, token=token)
48
  self.model = AutoModelForCausalLM.from_pretrained(
49
  self.model_id, device_map="auto", torch_dtype="auto", token=token
50
  )
 
51
  self.pipeline = pipeline(
52
  "text-generation",
53
  model=self.model,
54
  tokenizer=self.tokenizer,
55
  max_new_tokens=512,
56
+ temperature=0.2
57
  )
58
 
59
+ def forward(self, question: str) -> str:
60
+ prompt = f"""
61
+ You are an advanced reasoning assistant. Use Python code if helpful.
62
+
63
+ Instructions:
64
+ - Solve step-by-step.
65
+ - Wrap any code in <tool>...</tool>.
66
+ - End your answer with <final>...</final> with the final result only.
67
+
68
+ Example:
69
+ Question: What is 3 times the square root of 49?
70
+ Answer:
71
+ <tool>
72
+ import math
73
+ print(3 * math.sqrt(49))
74
+ </tool>
75
+ <final>21.0</final>
76
+
77
+ Now solve:
78
  Question: {question}
79
+ Answer:
80
+ """
81
 
82
  result = self.pipeline(prompt)[0]["generated_text"]
83
 
 
84
  if "<tool>" in result and "</tool>" in result:
85
  code = result.split("<tool>")[1].split("</tool>")[0].strip()
86
+ return self._run_code(code)
 
87
 
88
  elif "<final>" in result and "</final>" in result:
89
  final = result.split("<final>")[1].split("</final>")[0].strip()
 
91
 
92
  return "Could not determine how to respond. No <tool> or <final> block detected."
93
 
94
+ def _run_code(self, code: str) -> str:
95
+ buffer = io.StringIO()
96
+ try:
97
+ with contextlib.redirect_stdout(buffer):
98
+ exec(code, {})
99
+ return f"FINAL ANSWER (code output): {buffer.getvalue().strip()}"
100
+ except Exception as e:
101
+ return f"Error during code execution: {e}"
102
+
103
+
104
 
105
  #from smolagents import Tool
106
  #from langchain_community.document_loaders import WikipediaLoader
 
274
  final_answer_tool = FinalAnswerTool()
275
  video_transcription_tool = VideoTranscriptionTool()
276
 
277
+ # ✅ New Llama Tool
278
+ my_tool = CodeLlamaTool()
279
 
280
  system_prompt = f"""
281
  You are my general AI assistant. Your task is to answer the question I asked.