Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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,
|
|
|
30 |
|
31 |
class CodeLlamaTool(Tool):
|
32 |
name = "code_llama_tool"
|
33 |
-
description = "
|
34 |
|
35 |
inputs = {
|
36 |
"question": {
|
37 |
"type": "string",
|
38 |
-
"description": "The
|
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,
|
|
|
|
|
|
|
50 |
)
|
51 |
self.pipeline = pipeline(
|
52 |
"text-generation",
|
53 |
model=self.model,
|
54 |
tokenizer=self.tokenizer,
|
55 |
max_new_tokens=512,
|
56 |
-
|
57 |
-
|
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 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
84 |
-
except Exception
|
85 |
-
return f"Error
|
86 |
|
87 |
|
88 |
|
@@ -259,7 +287,7 @@ class BasicAgent:
|
|
259 |
video_transcription_tool = VideoTranscriptionTool()
|
260 |
|
261 |
# ✅ New Llama Tool
|
262 |
-
|
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 |
-
|
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 |
)
|