# model_tools.py import ollama import requests from bs4 import BeautifulSoup from transformers import pipeline # ---- LLM Task Extractor ---- # Load T5-based model once task_extractor = pipeline("text2text-generation", model="google/flan-t5-small") def extract_task(user_input): prompt = f"Classify the following ML task: {user_input}. Just reply with the task name." result = task_extractor(prompt, max_new_tokens=10) return result[0]["generated_text"].strip().lower() response = ollama.chat( model="mistral", # Replace with llama3, phi3, etc. if needed messages=[{"role": "user", "content": prompt}] ) return response['message']['content'].strip().lower() # ---- Hugging Face Scraper ---- def scrape_huggingface_models(task: str, max_results=5) -> list[dict]: """ Scrapes Hugging Face for top models for a given task. """ url = f"https://huggingface.co/models?pipeline_tag={task}&sort=downloads" try: resp = requests.get(url) soup = BeautifulSoup(resp.text, "html.parser") model_cards = soup.find_all("article", class_="model-card")[:max_results] results = [] for card in model_cards: name_tag = card.find("a", class_="model-link") model_name = name_tag.text.strip() if name_tag else "unknown" task_div = card.find("div", class_="task-tag") task_name = task_div.text.strip() if task_div else task arch = "encoder-decoder" if "bart" in model_name.lower() or "t5" in model_name.lower() else "unknown" results.append({ "model_name": model_name, "task": task_name, "architecture": arch }) return results except Exception as e: print(f"Scraping error: {e}") return []