wt002 commited on
Commit
48b54cf
·
verified ·
1 Parent(s): 94287ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -23
app.py CHANGED
@@ -25,17 +25,19 @@ 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"
@@ -46,43 +48,69 @@ class CodeLlamaTool(Tool):
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
- truncation=True,
57
- temperature=0.1
58
  )
59
 
60
  def forward(self, question: str) -> str:
61
- prompt = f"""
62
- "You are an assistant. Solve step-by-step. "
63
- "End your answer with a concise answer ina few words: FINAL ANSWER: [value]"
64
- """
65
 
66
- result = self.pipeline(prompt)[0]["generated_text"]
67
 
68
- if "<tool>" in result and "</tool>" in result:
69
- code = result.split("<tool>")[1].split("</tool>")[0].strip()
70
- return self._run_code(code)
71
 
72
- elif "<final>" in result and "</final>" in result:
73
- final = result.split("<final>")[1].split("</final>")[0].strip()
74
- return f"FINAL ANSWER: {final}"
 
 
 
 
 
75
 
76
- return "Could not determine how to respond. No <tool> or <final> block detected."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  def _run_code(self, code: str) -> str:
79
  buffer = io.StringIO()
80
  try:
81
  with contextlib.redirect_stdout(buffer):
82
  exec(code, {})
83
- return f"FINAL ANSWER (code output): {buffer.getvalue().strip()}"
84
- except Exception as e:
85
- return f"Error during code execution: {e}"
86
 
87
 
88
 
@@ -259,7 +287,7 @@ class BasicAgent:
259
  video_transcription_tool = VideoTranscriptionTool()
260
 
261
  # ✅ New Llama Tool
262
- my_tool = CodeLlamaTool()
263
 
264
  system_prompt = f"""
265
  You are my general AI assistant. Your task is to answer the question I asked.
@@ -278,7 +306,7 @@ If the answer is a comma-separated list, apply the above rules for each element
278
  keywords_extract_tool, speech_to_text_tool,
279
  visit_webpage_tool, final_answer_tool,
280
  parse_excel_to_json, video_transcription_tool,
281
- my_tool # 🔧 Add here
282
  ],
283
  add_base_tools=True
284
  )
 
25
 
26
  import io
27
  import contextlib
28
+ import traceback
29
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
30
+ from smolagents import Tool, CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, HfApiModel
31
+
32
 
33
  class CodeLlamaTool(Tool):
34
  name = "code_llama_tool"
35
+ description = "Solves reasoning/code questions using Meta Code Llama 7B Instruct"
36
 
37
  inputs = {
38
  "question": {
39
  "type": "string",
40
+ "description": "The question requiring code-based or reasoning-based solution"
41
  }
42
  }
43
  output_type = "string"
 
48
 
49
  self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, token=token)
50
  self.model = AutoModelForCausalLM.from_pretrained(
51
+ self.model_id,
52
+ device_map="auto",
53
+ torch_dtype="auto",
54
+ token=token
55
  )
56
  self.pipeline = pipeline(
57
  "text-generation",
58
  model=self.model,
59
  tokenizer=self.tokenizer,
60
  max_new_tokens=512,
61
+ temperature=0.2,
62
+ truncation=True
63
  )
64
 
65
  def forward(self, question: str) -> str:
66
+ prompt = f"""You are an AI that uses Python code to answer questions.
 
 
 
67
 
68
+ Question: {question}
69
 
70
+ Instructions:
71
+ - If solving requires code, use a block like <tool>code</tool>.
72
+ - Always end with <final>FINAL ANSWER</final> containing the final number or string.
73
 
74
+ Example:
75
+ Question: What is 5 * sqrt(36)?
76
+ Answer:
77
+ <tool>
78
+ import math
79
+ print(5 * math.sqrt(36))
80
+ </tool>
81
+ <final>30.0</final>
82
 
83
+ Answer:"""
84
+
85
+ response = self.pipeline(prompt)[0]["generated_text"]
86
+ return self.parse_and_execute(response)
87
+
88
+ def parse_and_execute(self, response: str) -> str:
89
+ try:
90
+ # Extract and run code if exists
91
+ if "<tool>" in response and "</tool>" in response:
92
+ code = response.split("<tool>")[1].split("</tool>")[0].strip()
93
+ result = self._run_code(code)
94
+ return f"FINAL ANSWER (code output): {result}"
95
+
96
+ # Extract final result directly
97
+ elif "<final>" in response and "</final>" in response:
98
+ final = response.split("<final>")[1].split("</final>")[0].strip()
99
+ return f"FINAL ANSWER: {final}"
100
+
101
+ return f"Could not extract final answer.\n\n{response}"
102
+
103
+ except Exception as e:
104
+ return f"Error in parse_and_execute: {str(e)}\n\nFull response:\n{response}"
105
 
106
  def _run_code(self, code: str) -> str:
107
  buffer = io.StringIO()
108
  try:
109
  with contextlib.redirect_stdout(buffer):
110
  exec(code, {})
111
+ return buffer.getvalue().strip()
112
+ except Exception:
113
+ return f"Error executing code:\n{traceback.format_exc()}"
114
 
115
 
116
 
 
287
  video_transcription_tool = VideoTranscriptionTool()
288
 
289
  # ✅ New Llama Tool
290
+ code_llama_tool = CodeLlamaTool()
291
 
292
  system_prompt = f"""
293
  You are my general AI assistant. Your task is to answer the question I asked.
 
306
  keywords_extract_tool, speech_to_text_tool,
307
  visit_webpage_tool, final_answer_tool,
308
  parse_excel_to_json, video_transcription_tool,
309
+ code_llama_tool # 🔧 Add here
310
  ],
311
  add_base_tools=True
312
  )