FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
replicate-python/replicate/files.py at main · vinstics/replicate-python · GitHub
vinstics
/
replicate-python
Public
forked from
creatorrr/replicate-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
replicate-python
/
replicate
/
files.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (64 loc) · 2.06 KB
Breadcrumbs
replicate-python
/
replicate
/
files.py
Copy path
File metadata and controls
84 lines (64 loc) · 2.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import
base64
import
io
import
mimetypes
import
os
import
httpx
import
requests
def
to_data_url
(
fh
:
io
.
IOBase
)
->
str
:
"""
Lifted straight from cog.files
"""
fh
.
seek
(
0
)
b
=
fh
.
read
()
# The file handle is strings, not bytes
if
isinstance
(
b
,
str
):
b
=
b
.
encode
(
"utf-8"
)
encoded_body
=
base64
.
b64encode
(
b
)
if
getattr
(
fh
,
"name"
,
None
):
# despite doing a getattr check here, mypy complains that io.IOBase has no attribute name
mime_type
=
mimetypes
.
guess_type
(
fh
.
name
)[
0
]
# type: ignore
else
:
mime_type
=
"application/octet-stream"
s
=
encoded_body
.
decode
(
"utf-8"
)
return
f"data:
{
mime_type
}
;base64,
{
s
}
"
def
upload_file_to_server
(
fh
:
io
.
IOBase
,
output_file_prefix
:
str
)
->
str
:
"""
Lifted straight from cog.files
"""
fh
.
seek
(
0
)
name
=
getattr
(
fh
,
"name"
,
"output"
)
url
=
output_file_prefix
+
os
.
path
.
basename
(
name
)
resp
=
requests
.
put
(
url
,
files
=
{
"file"
:
fh
})
resp
.
raise_for_status
()
return
url
def
upload_file
(
fh
:
io
.
IOBase
,
output_file_prefix
:
str
=
None
)
->
str
:
"""
Lifted straight from cog.files
"""
fh
.
seek
(
0
)
if
output_file_prefix
is
not
None
:
url
=
upload_file_to_server
(
fh
,
output_file_prefix
)
return
url
data_url
:
str
=
to_data_url
(
fh
)
return
data_url
async
def
upload_file_to_server_async
(
fh
:
io
.
IOBase
,
output_file_prefix
:
str
)
->
str
:
"""
Lifted straight from cog.files
"""
fh
.
seek
(
0
)
name
=
getattr
(
fh
,
"name"
,
"output"
)
url
=
output_file_prefix
+
os
.
path
.
basename
(
name
)
# httpx does not follow redirects by default
async
with
httpx
.
AsyncClient
(
follow_redirects
=
True
)
as
client
:
resp
=
await
client
.
put
(
url
,
files
=
{
"file"
:
fh
})
return
url
async
def
upload_file_async
(
fh
:
io
.
IOBase
,
output_file_prefix
:
str
=
None
)
->
str
:
"""
Lifted straight from cog.files
"""
fh
.
seek
(
0
)
if
output_file_prefix
is
not
None
:
url
=
await
upload_file_to_server_async
(
fh
,
output_file_prefix
)
return
url
data_url
:
str
=
to_data_url
(
fh
)
return
data_url
Back
|
FazBrowse Home
|
New Git URL