Spaces:
Sleeping
Sleeping
Update agent.py
Browse files6 unmodified lines789101112131415161711 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465666768697071724 unmodified lines777879808182836 unmodified linesimport osimport whisperSYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Think step by step to find the best possible answer.Then return only the answer without any explanation or formatting.Do not say 'Final answer' or anything else. Just output the raw answer string."""class GaiaAgent:11 unmodified linesif results:return results[0]['body']except Exception as e:return f"Search failed: {e}"return ""def transcribe_audio(self, file_path: str) -> str:try:result = self.transcriber.transcribe(file_path)return result['text']except Exception as e:return f"Audio transcription failed: {e}"def handle_excel(self, file_path: str) -> str:try:df = pd.read_excel(file_path)food_sales = df[df['Category'].str.lower() != 'drink']['Sales'].sum()return f"{food_sales:.2f}"except Exception as e:return f"Excel parsing failed: {e}"def __call__(self, question: str, files: dict = None) -> tuple[str, str]:try:if "http" in question or "Wikipedia" in question:web_context = self.search(question)prompt = f"{SYSTEM_PROMPT}\n\n{web_context}\n\nQuestion: {question}"elif files:file_keys = list(files.keys())for key in file_keys:if key.endswith(".mp3"):audio_txt = self.transcribe_audio(files[key])prompt = f"{SYSTEM_PROMPT}\n\n{audio_txt}\n\n{question}"breakelif key.endswith(".xlsx"):excel_result = self.handle_excel(files[key])return excel_result, excel_resultelse:prompt = f"{SYSTEM_PROMPT}\n\n{question}"else:prompt = f"{SYSTEM_PROMPT}\n\n{question}"inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True).to(self.device)outputs = self.model.generate(**inputs,4 unmodified lines)output_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)final = output_text.strip()return final, output_textexcept Exception as e:return "ERROR", f"Agent failed: {e}"6 unmodified lines789101112131411 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960616263646566674 unmodified lines727374757677786 unmodified linesimport osimport whisperSYSTEM_PROMPT = """You are a helpful AI assistant. Think step by step to solve the problem. If the question requires reasoning, perform it. If it refers to a search or file, use the result provided. At the end, return ONLY the final answer string. No explanations."""class GaiaAgent:11 unmodified linesif results:return results[0]['body']except Exception as e:return ""return ""def transcribe_audio(self, file_path: str) -> str:try:result = self.transcriber.transcribe(file_path)return result['text']except Exception:return ""def handle_excel(self, file_path: str) -> str:try:df = pd.read_excel(file_path)df.columns = [col.lower() for col in df.columns]if 'category' in df.columns and 'sales' in df.columns:food_sales = df[df['category'].str.lower() != 'drink']['sales'].sum()return f"{food_sales:.2f}"except Exception:return ""return ""def __call__(self, question: str, files: dict = None) -> tuple[str, str]:try:context = ""if files:for filename, filepath in files.items():if filename.endswith(".mp3"):context = self.transcribe_audio(filepath)breakelif filename.endswith(".xlsx"):excel_result = self.handle_excel(filepath)return excel_result.strip(), excel_result.strip()elif "http" in question.lower() or "wikipedia" in question.lower():context = self.search(question)prompt = f"{SYSTEM_PROMPT}\n\n{context}\n\nQuestion: {question.strip()}"inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True).to(self.device)outputs = self.model.generate(**inputs,4 unmodified lines)output_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)final = output_text.strip()return final, finalexcept Exception as e:return "ERROR", f"Agent failed: {e}"
agent.py+23-28