Spaces:
Runtime error
Runtime error
Download codeinterpreterapi/schema/file.py from brzhang/codeinterpreter: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/brzhang/codeinterpreter/resolve/e0cbbfe8ec5bf653874aceb78f2f35a2b41ee7c7/codeinterpreterapi/schema/file.py
- Command line
-
hf download hf://spaces/brzhang/codeinterpreter@e0cbbfe8ec5bf653874aceb78f2f35a2b41ee7c7/codeinterpreterapi/schema/file.py
-
curl -L -o file.py https://huggingface.co/spaces/brzhang/codeinterpreter/resolve/e0cbbfe8ec5bf653874aceb78f2f35a2b41ee7c7/codeinterpreterapi/schema/file.py
1.58 kB
| import asyncio | |
| from pydantic import BaseModel | |
| class File(BaseModel): | |
| name: str | |
| content: bytes | |
| def from_path(cls, path: str): | |
| with open(path, "rb") as f: | |
| path = path.split("/")[-1] | |
| return cls(name=path, content=f.read()) | |
| async def afrom_path(cls, path: str): | |
| await asyncio.to_thread(cls.from_path, path) | |
| def from_url(cls, url: str): | |
| import requests # type: ignore | |
| r = requests.get(url) | |
| return cls(name=url.split("/")[-1], content=r.content) | |
| async def afrom_url(cls, url: str): | |
| import aiohttp | |
| async with aiohttp.ClientSession() as session: | |
| async with session.get(url) as r: | |
| return cls(name=url.split("/")[-1], content=await r.read()) | |
| def save(self, path: str): | |
| with open(path, "wb") as f: | |
| f.write(self.content) | |
| async def asave(self, path: str): | |
| await asyncio.to_thread(self.save, path) | |
| def show_image(self): | |
| try: | |
| from PIL import Image # type: ignore | |
| except ImportError: | |
| print( | |
| "Please install it with `pip install codeinterpreterapi[image_support]` to display images." | |
| ) | |
| exit(1) | |
| from io import BytesIO | |
| img_io = BytesIO(self.content) | |
| img = Image.open(img_io) | |
| # Display the image | |
| img.show() | |
| def __str__(self): | |
| return self.name | |
| def __repr__(self): | |
| return f"File(name={self.name})" | |