Spaces:
Sleeping
Sleeping
Update agent.py
Browse files12344 unmodified lines9101112131415161718192021222324252627282930313233343536from transformers import pipelineSYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:4 unmodified linesIf you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""class GaiaAgent:def __init__(self, model_id="google/flan-t5-base"):self.generator = pipeline("text2text-generation",model=model_id,tokenizer=model_id,max_new_tokens=512,device="cpu")def __call__(self, question: str) -> tuple[str, str]:try:prompt = f"{SYSTEM_PROMPT}\n\n{question}\nFINAL ANSWER:"output = self.generator(prompt)[0]["generated_text"]if "FINAL ANSWER:" in output:final = output.split("FINAL ANSWER:")[-1].strip().split("\n")[0].strip()else:final = output.strip()return final, outputexcept Exception as e:return "ERROR", f"Agent failed: {e}"123454 unmodified lines10111213141516171819202122232425262728293031323334353637383940from transformers import AutoTokenizer, AutoModelForCausalLM, pipelineimport torchSYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:4 unmodified linesIf you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""class GaiaAgent:def __init__(self, model_id="tiiuae/falcon-rw-1b"):self.tokenizer = AutoTokenizer.from_pretrained(model_id)self.model = AutoModelForCausalLM.from_pretrained(model_id)self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")self.model.to(self.device)def __call__(self, question: str) -> tuple[str, str]:try:prompt = f"{SYSTEM_PROMPT}\n\n{question}\nFINAL ANSWER:"inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)outputs = self.model.generate(**inputs,max_new_tokens=256,do_sample=True,temperature=0.7,pad_token_id=self.tokenizer.eos_token_id)output_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)if "FINAL ANSWER:" in output_text:final = output_text.split("FINAL ANSWER:")[-1].strip().split("\n")[0].strip()else:final = output_text.strip()return final, output_textexcept Exception as e:return "ERROR", f"Agent failed: {e}"
agent.py+20-16