lightning / app.py
sharktide's picture
Create app.py
0048c63 verified
Raw
History Blame
1.58 kB
# main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import httpx
from bs4 import BeautifulSoup
from typing import List, Dict
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["GET"],
allow_headers=["*"],
)
OLLAMA_LIBRARY_URL = "https://ollama.com/library"
@app.get("/models")
async def get_models() -> List[Dict]:
async with httpx.AsyncClient() as client:
response = await client.get(OLLAMA_LIBRARY_URL)
html = response.text
soup = BeautifulSoup(html, "html.parser")
items = soup.select("li[x-test-model]")
models = []
for item in items:
name = item.select_one("[x-test-model-title] span")
description = item.select_one("p.max-w-lg")
sizes = [el.get_text(strip=True) for el in item.select("[x-test-size]")]
pulls = item.select_one("[x-test-pull-count]")
tags = [t.get_text(strip=True) for t in item.select('span[class*="text-blue-600"]')]
updated = item.select_one("[x-test-updated]")
link = item.select_one("a")
models.append({
"name": name.get_text(strip=True) if name else "",
"description": description.get_text(strip=True) if description else "No description",
"sizes": sizes,
"pulls": pulls.get_text(strip=True) if pulls else "Unknown",
"tags": tags,
"updated": updated.get_text(strip=True) if updated else "Unknown",
"link": link.get("href") if link else None,
})
return models