utils.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from fastapi import APIRouter, UploadFile, File, BackgroundTasks
  2. from fastapi import Depends, HTTPException, status
  3. from starlette.responses import StreamingResponse
  4. from pydantic import BaseModel
  5. import requests
  6. import os
  7. import aiohttp
  8. import json
  9. from utils.misc import calculate_sha256
  10. from config import OLLAMA_API_BASE_URL
  11. router = APIRouter()
  12. class UploadBlobForm(BaseModel):
  13. filename: str
  14. from urllib.parse import urlparse
  15. def parse_huggingface_url(hf_url):
  16. # Parse the URL
  17. parsed_url = urlparse(hf_url)
  18. # Get the path and split it into components
  19. path_components = parsed_url.path.split("/")
  20. # Extract the desired output
  21. user_repo = "/".join(path_components[1:3])
  22. model_file = path_components[-1]
  23. return [user_repo, model_file]
  24. async def download_file_stream(url, file_path, chunk_size=1024 * 1024):
  25. done = False
  26. if os.path.exists(file_path):
  27. current_size = os.path.getsize(file_path)
  28. else:
  29. current_size = 0
  30. headers = {"Range": f"bytes={current_size}-"} if current_size > 0 else {}
  31. timeout = aiohttp.ClientTimeout(total=60) # Set the timeout
  32. async with aiohttp.ClientSession(timeout=timeout) as session:
  33. async with session.get(url, headers=headers) as response:
  34. total_size = int(response.headers.get("content-length", 0)) + current_size
  35. with open(file_path, "ab+") as file:
  36. async for data in response.content.iter_chunked(chunk_size):
  37. current_size += len(data)
  38. file.write(data)
  39. done = current_size == total_size
  40. progress = round((current_size / total_size) * 100, 2)
  41. yield f'data: {{"progress": {progress}, "current": {current_size}, "total": {total_size}}}\n\n'
  42. if done:
  43. file.seek(0)
  44. hashed = calculate_sha256(file)
  45. file.seek(0)
  46. url = f"{OLLAMA_API_BASE_URL}/blobs/sha256:{hashed}"
  47. response = requests.post(url, data=file)
  48. if response.ok:
  49. res = {
  50. "done": done,
  51. "blob": f"sha256:{hashed}",
  52. }
  53. os.remove(file_path)
  54. yield f"data: {json.dumps(res)}\n\n"
  55. else:
  56. raise "Ollama: Could not create blob, Please try again."
  57. @router.get("/download")
  58. async def download(
  59. url: str = "https://huggingface.co/TheBloke/stablelm-zephyr-3b-GGUF/resolve/main/stablelm-zephyr-3b.Q2_K.gguf",
  60. ):
  61. user_repo, model_file = parse_huggingface_url(url)
  62. os.makedirs("./uploads", exist_ok=True)
  63. file_path = os.path.join("./uploads", f"{model_file}")
  64. return StreamingResponse(
  65. download_file_stream(url, file_path), media_type="text/event-stream"
  66. )
  67. @router.post("/upload")
  68. async def upload(file: UploadFile = File(...)):
  69. os.makedirs("./uploads", exist_ok=True)
  70. file_path = os.path.join("./uploads", file.filename)
  71. async def file_write_stream():
  72. total = 0
  73. total_size = file.size
  74. chunk_size = 1024 * 1024
  75. done = False
  76. try:
  77. with open(file_path, "wb+") as f:
  78. while True:
  79. chunk = file.file.read(chunk_size)
  80. if not chunk:
  81. break
  82. f.write(chunk)
  83. total += len(chunk)
  84. done = total_size == total
  85. res = {
  86. "total": total_size,
  87. "uploaded": total,
  88. }
  89. yield f"data: {json.dumps(res)}\n\n"
  90. if done:
  91. f.seek(0)
  92. hashed = calculate_sha256(f)
  93. f.seek(0)
  94. url = f"{OLLAMA_API_BASE_URL}/blobs/sha256:{hashed}"
  95. response = requests.post(url, data=f)
  96. if response.ok:
  97. res = {
  98. "done": done,
  99. "blob": f"sha256:{hashed}",
  100. }
  101. os.remove(file_path)
  102. yield f"data: {json.dumps(res)}\n\n"
  103. else:
  104. raise "Ollama: Could not create blob, Please try again."
  105. except Exception as e:
  106. res = {"error": str(e)}
  107. yield f"data: {json.dumps(res)}\n\n"
  108. return StreamingResponse(file_write_stream(), media_type="text/event-stream")