Spaces:
Sleeping
Sleeping
Update agent.py
Browse files12345678910115 unmodified lines171819202122231 unmodified line2526272829303132333435363714 unmodified lines525354555657588 unmodified lines676869707172731 unmodified line7576777879import torchfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLMfrom ddgs import DDGSimport reimport pandas as pdimport tempfileimport 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.5 unmodified linesself.model = AutoModelForSeq2SeqLM.from_pretrained(model_id)self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")self.model.to(self.device)self.transcriber = whisper.load_model("base")def search(self, query: str) -> str:try:1 unmodified lineresults = list(ddgs.text(query, safesearch="off"))if 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 ""14 unmodified linescontext = ""if files:for filename, filepath in files.items():if filename.endswith(".mp3"):context = self.transcribe_audio(filepath)breakelif filename.endswith(".xlsx"):8 unmodified lines**inputs,max_new_tokens=128,do_sample=False,temperature=0.0,pad_token_id=self.tokenizer.pad_token_id)output_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)1 unmodified linereturn final, finalexcept Exception as e:return "ERROR", f"Agent failed: {e}"12345678910111213145 unmodified lines202122232425262728293031323334353637381 unmodified line4041424344454647484950515214 unmodified lines676869707172738 unmodified lines8283848586871 unmodified line89909192import torchfrom transformers import (AutoTokenizer,AutoModelForSeq2SeqLM,pipeline,AutoProcessor,AutoModelForSpeechSeq2Seq)from ddgs import DDGSimport pandas as pdimport osSYSTEM_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.5 unmodified linesself.model = AutoModelForSeq2SeqLM.from_pretrained(model_id)self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")self.model.to(self.device)# Whisper via HFself.asr_model_id = "openai/whisper-small"self.asr_processor = AutoProcessor.from_pretrained(self.asr_model_id)self.asr_model = AutoModelForSpeechSeq2Seq.from_pretrained(self.asr_model_id).to(self.device)self.pipe = pipeline("automatic-speech-recognition",model=self.asr_model,tokenizer=self.asr_processor.tokenizer,feature_extractor=self.asr_processor.feature_extractor,return_timestamps=False,device=0 if torch.cuda.is_available() else -1)def search(self, query: str) -> str:try:1 unmodified lineresults = list(ddgs.text(query, safesearch="off"))if results:return results[0]['body']except Exception:return ""return ""def transcribe_audio(self, file_path: str) -> str:try:result = self.pipe(file_path)return result['text']except Exception:return ""14 unmodified linescontext = ""if files:for filename, filepath in files.items():if filename.endswith(".mp3") or filename.endswith(".wav"):context = self.transcribe_audio(filepath)breakelif filename.endswith(".xlsx"):8 unmodified lines**inputs,max_new_tokens=128,do_sample=False,pad_token_id=self.tokenizer.pad_token_id)output_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)1 unmodified linereturn final, finalexcept Exception as e:return "ERROR", f"Agent failed: {e}"
agent.py+23-10