Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import httpx
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
from typing import List, Dict
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_methods=["GET"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
OLLAMA_LIBRARY_URL = "https://ollama.com/library"
|
| 18 |
+
|
| 19 |
+
@app.get("/models")
|
| 20 |
+
async def get_models() -> List[Dict]:
|
| 21 |
+
async with httpx.AsyncClient() as client:
|
| 22 |
+
response = await client.get(OLLAMA_LIBRARY_URL)
|
| 23 |
+
html = response.text
|
| 24 |
+
|
| 25 |
+
soup = BeautifulSoup(html, "html.parser")
|
| 26 |
+
items = soup.select("li[x-test-model]")
|
| 27 |
+
|
| 28 |
+
models = []
|
| 29 |
+
for item in items:
|
| 30 |
+
name = item.select_one("[x-test-model-title] span")
|
| 31 |
+
description = item.select_one("p.max-w-lg")
|
| 32 |
+
sizes = [el.get_text(strip=True) for el in item.select("[x-test-size]")]
|
| 33 |
+
pulls = item.select_one("[x-test-pull-count]")
|
| 34 |
+
tags = [t.get_text(strip=True) for t in item.select('span[class*="text-blue-600"]')]
|
| 35 |
+
updated = item.select_one("[x-test-updated]")
|
| 36 |
+
link = item.select_one("a")
|
| 37 |
+
|
| 38 |
+
models.append({
|
| 39 |
+
"name": name.get_text(strip=True) if name else "",
|
| 40 |
+
"description": description.get_text(strip=True) if description else "No description",
|
| 41 |
+
"sizes": sizes,
|
| 42 |
+
"pulls": pulls.get_text(strip=True) if pulls else "Unknown",
|
| 43 |
+
"tags": tags,
|
| 44 |
+
"updated": updated.get_text(strip=True) if updated else "Unknown",
|
| 45 |
+
"link": link.get("href") if link else None,
|
| 46 |
+
})
|
| 47 |
+
|
| 48 |
+
return models
|