Spaces:
Sleeping
Sleeping
Update agent.py
Browse files122345678941061181213145 unmodified lines20212220232425262728293031323334353637381 unmodified line40414228434445464748344950515214 unmodified lines67686955707172738 unmodified lines828384708586871 unmodified line8990917892import torchfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLMfrom transformers import ( AutoTokenizer, AutoModelForSeq2SeqLM, pipeline, AutoProcessor, AutoModelForSpeechSeq2Seq)from ddgs import DDGSimport reimport pandas as pdimport tempfileimport osimport whisper
SYSTEM_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 lines self.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")
# Whisper via HF self.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 line results = list(ddgs.text(query, safesearch="off")) if results: return results[0]['body'] except Exception as e: except Exception: return "" return ""
def transcribe_audio(self, file_path: str) -> str: try: result = self.transcriber.transcribe(file_path) result = self.pipe(file_path) return result['text'] except Exception: return ""14 unmodified lines context = "" if files: for filename, filepath in files.items(): if filename.endswith(".mp3"): if filename.endswith(".mp3") or filename.endswith(".wav"): context = self.transcribe_audio(filepath) break elif 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 line return final, final except Exception as e: return "ERROR", f"Agent failed: {e}"
agent.py+23-10