File size: 562 Bytes
6155c0e
fcfec80
 
6155c0e
fcfec80
 
 
 
 
6155c0e
 
fcfec80
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import gradio as gr
from typing import Dict
import base64

def encode_file_to_base64(f):
    with open(f, "rb") as file:
        encoded_string = base64.b64encode(file.read())
        base64_str = str(encoded_string, "utf-8")
        return base64_str

class CustomFile(gr.File):
    def get_block_name(self) -> str:
        return "file"

    def postprocess(self, y: str) -> Dict:
        res = super().postprocess(y)
        if res is not None:
            for file in res:
                file['data'] = encode_file_to_base64(file['name'])
        return res