Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
27 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def __init__(self):
|
34 |
-
self.model_id = "
|
35 |
-
token = os.getenv("HF_TOKEN")
|
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.
|
48 |
)
|
49 |
|
50 |
-
def
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
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 |
-
|
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
|
254 |
-
my_tool =
|
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.
|